Skip to content

Instantly share code, notes, and snippets.

View Shuhala's full-sized avatar

Sophie Bernadin-Mercier Shuhala

  • Montreal
View GitHub Profile
@Shuhala
Shuhala / createmenu.java
Created June 21, 2017 12:46
Creates a JMenu filled with a bunch of JMenuItems
/**
* Creates a JMenu with his JMenuItems
*
* @param titleKey JMenu name
* @param itemKeys JMenuItems name
* @return menu
*/
private static JMenu createJMenu(String titleKey, String[] itemKeys) {
JMenu menu = new JMenu(titleKey);
Arrays.stream(itemKeys).forEach(item -> menu.add(new JMenuItem(item)));
@Shuhala
Shuhala / AppConfig.java
Created July 6, 2017 18:43
Static class to load and get *.properties file values
package framework.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public final class AppConfig {
private static Properties properties;
@Shuhala
Shuhala / optionalOrElse.java
Created July 6, 2017 18:50
Optional.ofNullable example. Get value if not null, otherwise return something else.
Optional.ofNullable(properties.getProperty(key))
.filter(value -> value != null || !value.isEmpty())
.orElse(AnotherValue);
@Shuhala
Shuhala / outdated-requirements.sh
Created July 25, 2017 13:31
Output latest outdated pip dependencies version in requirements.txt format
pip list --outdated | sed 's/ (.*Latest: /==/g;s/ \[.*//g'
@Shuhala
Shuhala / property_type_mixin.py
Created February 20, 2019 19:58
PropertyTypeMixin
from typing import Any, Dict
class PropertyTypeMixin:
"""
Enforce type checking of attributes at runtime
"""
_properties_type: Dict[str, Any] = {}
def __setattr__(self, key, value):
@Shuhala
Shuhala / argparse_dict.py
Created March 25, 2019 12:33
Dictionary type for argparse argument
parser = argparse.ArgumentParser(description="input `a=b&c=d` becomes `dict({'a':'b', 'c':'d'})`")
parser.add_argument(
"-p", "--params",
help="Convert parameters in the form `--params a=b&c=d` to a dictionary",
action=type('', (argparse.Action,), dict(
__call__=lambda a, _, n, v, __: setattr(n, a.dest, dict([kv.split('=') for kv in v.split('&')]))
)),
default={},
)
@Shuhala
Shuhala / find_all.py
Created May 20, 2019 22:20
SQLAlchemy findall
def find_all(session: Session, offset: int, limit: int, order_by: str = "", filters: Optional[list] = None, joins: Optional[list] = None):
if not filters:
filters = []
if not joins:
joins = []
query = session.query(self._model_type)
if joins:
query = query.join(*joins)
@Shuhala
Shuhala / alembic_migrations.py
Created November 20, 2019 19:37
Scripts for alembic to assert that the database is up to date, migrations are up to date and to run migrations programmatically
from alembic import config
from alembic import script
from alembic.autogenerate import compare_metadata
from alembic.runtime import migration
from alembic.runtime.environment import EnvironmentContext
# other imports ...
def assert_database_is_up_to_date():
""" Database is up to date with migration head version """
import traceback
from typing import Optional, Union
def format_exception(exception: Optional[Union[Exception, BaseException]]) -> str:
""" Format a prettier exception trace """
if exception:
return f"\n{type(exception).__name__} Exception: {exception}" f"\n{''.join(traceback.format_tb(exception.__traceback__))}"
return " Exception."
import traceback
from typing import Optional, Union
def format_exception(exception: Optional[Union[Exception, BaseException]]) -> str:
""" Format a prettier exception trace """
if exception:
return f"\n{type(exception).__name__} Exception: {exception}" f"\n{''.join(traceback.format_tb(exception.__traceback__))}"
return " Exception."