Skip to content

Instantly share code, notes, and snippets.

use anyhow::{Context};
use std::fs::File;
use std::io::{Read};
fn read_file_contents(path: &str) -> tomercode_error::Result<String> {
let contents = open_file(path).with_context(|| format!("Failed to open file: {}", path))?;
let processed_contents = process_contents(&contents).context("could not process file contents")?;
Ok(processed_contents)
}
struct Person {
name: String,
age: u32,
address: String,
}
pub type Result<T, E = TomerCodeError> = anyhow::Result<T, E>;
#[derive(Error, Debug)]
pub enum TomerCodeError {
#[error("Invalid state error: {0}")]
InvalidState(anyhow::Error),
#[error("Connection Error Occoured: {0}")]
ConnectionError(String),
fn log(value: &String) -> Result<(), LoggingError> {
if value.len() > 10 {
return Err(LoggingError::TooLong);
}
println!("{}", value);
Ok(())
}
fn get_registry_value(key_path: &str) -> Result<String, io::Error> {
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
#[derive(Error, Debug)]
pub enum TomerCodeError {
#[error("Invalid state error: {0}")]
InvalidState(String),
#[error("Connection Error Occoured: {0}")]
ConnectionError(String),
#[error("Registry Error Occoured: {0}")]
RegistyError(#[from] io::Error),
fn get_registry_value(key_path: &str) -> Result<String, io::Error> {
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
let key = hklm.open_subkey(key_path)?;
let value: String = key.get_value("my_key")?;
Ok(value)
}
fn some_or_go_get_from_db(some_value: Option<i32>) {
let value = some_value.or_else(|| db.get());
println!("Value: {}", value);
}
# First Project (ModuleA)
class ModuleA:
def __init__(self, module_b: ModuleB, module_c: ModuleC):
print('im code from module A')
module_b.hello()
module_c.hello()
def hello(self):
print('hello from A')
@TomerCohen95
TomerCohen95 / vms_cleanup.py
Last active September 27, 2019 09:52
fifth and sixth layer
def was_created_today(machine: vim.VirtualMachine) -> bool:
date = _get_date_from_name(machine.name)
# return some implemetation that checks if date is today
def _get_date_from_name(name: str) -> datetime.date:
# return some implementation that extracts date from name of machine
@TomerCohen95
TomerCohen95 / vms_cleanup.py
Created September 16, 2019 05:24
forth layer
def was_created_today(machine) -> bool:
date = _get_date_from_name(machine.name)
date_now = datetime.datetime.now().date()
return (date_now - date).days > 0