code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def _execute(self, logger: Logger, options: Dict[str, Dict[str, Any]]) -> T:
if isinstance(self.parser, _BaseParser):
if (not self.is_singlefile) and self.parser.supports_multifile():
return self.parser._parse_multifile(self.obj_type, self.obj_on_fs_to_parse,
... | Implementation of the parent class method.
Checks that self.parser is a _BaseParser, and calls the appropriate parsing method.
:param logger:
:param options:
:return: |
def create_parsing_plan(self, desired_type: Type[T], filesystem_object: PersistedObject, logger: Logger,
_main_call: bool = True):
in_root_call = False
# -- log msg only for the root call, not for the children that will be created by the code below
if _main_... | Implements the abstract parent method by using the recursive parsing plan impl. Subclasses wishing to produce
their own parsing plans should rather override _create_parsing_plan in order to benefit from this same log msg.
:param desired_type:
:param filesystem_object:
:param logger:
... |
def _create_parsing_plan(self, desired_type: Type[T], filesystem_object: PersistedObject, logger: Logger,
log_only_last: bool = False):
logger.debug('(B) ' + get_parsing_plan_log_str(filesystem_object, desired_type,
log... | Adds a log message and creates a recursive parsing plan.
:param desired_type:
:param filesystem_object:
:param logger:
:param log_only_last: a flag to only log the last part of the file path (default False)
:return: |
def _get_parsing_plan_for_multifile_children(self, obj_on_fs: PersistedObject, desired_type: Type[T],
logger: Logger) -> Dict[str, ParsingPlan[T]]:
pass | This method is called by the _RecursiveParsingPlan when created.
Implementing classes should return a dictionary containing a ParsingPlan for each child they plan to parse
using this framework. Note that for the files that will be parsed using a parsing library it is not necessary to
return a Pa... |
def _parse_singlefile(self, desired_type: Type[T], file_path: str, encoding: str, logger: Logger,
options: Dict[str, Dict[str, Any]]) -> T:
raise Exception('Not implemented since this is a MultiFileParser') | Implementation of the parent method : since this is a multifile parser, this is not implemented.
:param desired_type:
:param file_path:
:param encoding:
:param logger:
:param options:
:return: |
def create(parser_func: Union[ParsingMethodForStream, ParsingMethodForFile], caught: Exception):
msg = 'Caught TypeError while calling parsing function \'' + str(parser_func.__name__) + '\'. ' \
'Note that the parsing function signature should be ' + parsing_method_stream_example_signatur... | Helper method provided because we actually can't put that in the constructor, it creates a bug in Nose tests
https://github.com/nose-devs/nose/issues/725
:param parser_func:
:param caught:
:return: |
def _parse_singlefile(self, desired_type: Type[T], file_path: str, encoding: str, logger: Logger,
options: Dict[str, Dict[str, Any]]) -> T:
opts = get_options_for_id(options, self.get_id_for_options())
if self._streaming_mode:
# We open the stream, and le... | Relies on the inner parsing function to parse the file.
If _streaming_mode is True, the file will be opened and closed by this method. Otherwise the parsing function
will be responsible to open and close.
:param desired_type:
:param file_path:
:param encoding:
:param opt... |
def queryByPortSensor(portiaConfig, edgeId, port, sensor, strategy=SummaryStrategies.PER_HOUR, interval=1, params={ 'from': None, 'to': None, 'order': None, 'precision': 'ms', 'fill':'none', 'min': True, 'max': True, 'sum': True, 'avg': True, 'median': False, 'mode': False, 'stddev': False, 'spread': False }):
... | Returns a pandas data frame with the portia select resultset |
def _process_counter_example(self, mma, w_string):
Process a counterexample in the Rivest-Schapire way.
Args:
mma (DFA): The hypothesis automaton
w_string (str): The examined string to be consumed
Returns:
None
"""
diff = len(w_string)... | Process a counterexample in the Rivest-Schapire way.
Args:
mma (DFA): The hypothesis automaton
w_string (str): The examined string to be consumed
Returns:
None |
def get_dfa_conjecture(self):
dfa = DFA(self.alphabet)
for s in self.observation_table.sm_vector:
for i in self.alphabet:
dst = self.observation_table.equiv_classes[s + i]
# If dst == None then the table is not closed.
if dst == None:
... | Utilize the observation table to construct a Mealy Machine.
The library used for representing the Mealy Machine is the python
bindings of the openFST library (pyFST).
Args:
None
Returns:
MealyMachine: A mealy machine build based on a closed and consistent
... |
def _init_table(self):
self.observation_table.sm_vector.append(self.epsilon)
self.observation_table.smi_vector = list(self.alphabet)
self.observation_table.em_vector.append(self.epsilon)
self._fill_table_entry(self.epsilon, self.epsilon)
for s in self.observation_table.... | Initialize the observation table. |
def learn_dfa(self, mma=None):
logging.info('Initializing learning procedure.')
if mma:
self._init_table_from_dfa(mma)
else:
self._init_table()
logging.info('Generating a closed and consistent observation table.')
while True:
closed ... | Implements the high level loop of the algorithm for learning a
Mealy machine.
Args:
mma (DFA): The input automaton
Returns:
MealyMachine: A string and a model for the Mealy machine to be learned. |
def print_error_to_io_stream(err: Exception, io: TextIOBase, print_big_traceback : bool = True):
if print_big_traceback:
traceback.print_tb(err.__traceback__, file=io, limit=-GLOBAL_CONFIG.multiple_errors_tb_limit)
else:
traceback.print_tb(err.__traceback__, file=io, limit=-1)
io.writel... | Utility method to print an exception's content to a stream
:param err:
:param io:
:param print_big_traceback:
:return: |
def should_hide_traceback(e):
if type(e) in {WrongTypeCreatedError, CascadeError, TypeInformationRequiredError}:
return True
elif type(e).__name__ in {'InvalidAttributeNameForConstructorError', 'MissingMandatoryAttributeFiles'}:
return True
else:
return False | Returns True if we can hide the error traceback in the warnings messages |
def _get_parsing_plan_for_multifile_children(self, obj_on_fs: PersistedObject, desired_type: Type[Any],
logger: Logger) -> Dict[str, Any]:
raise Exception('This should never happen, since this parser relies on underlying parsers') | Implementation of AnyParser API |
def _parse_multifile(self, desired_type: Type[T], obj: PersistedObject,
parsing_plan_for_children: Dict[str, AnyParser._RecursiveParsingPlan],
logger: Logger, options: Dict[str, Dict[str, Any]]) -> T:
raise Exception('This should never happen, since thi... | Implementation of AnyParser API |
def _create_parsing_plan(self, desired_type: Type[T], filesystem_object: PersistedObject, logger: Logger,
log_only_last: bool = False) -> ParsingPlan[T]:
# build the parsing plan
logger.debug('(B) ' + get_parsing_plan_log_str(filesystem_object, desired_type,
... | Creates a parsing plan to parse the given filesystem object into the given desired_type.
This overrides the method in AnyParser, in order to provide a 'cascading' parsing plan
:param desired_type:
:param filesystem_object:
:param logger:
:param log_only_last: a flag to only log ... |
def _parse_singlefile(self, desired_type: Type[T], file_path: str, encoding: str, logger: Logger,
options: Dict[str, Dict[str, Any]]) -> T:
# first use the base parser to parse something compliant with the conversion chain
first = self._base_parser._parse_singlefile(se... | Implementation of AnyParser API |
def _get_parsing_plan_for_multifile_children(self, obj_on_fs: PersistedObject, desired_type: Type[Any],
logger: Logger) -> Dict[str, Any]:
return self._base_parser._get_parsing_plan_for_multifile_children(obj_on_fs, self._converter.from_type, logger) | Implementation of AnyParser API |
def _parse_multifile(self, desired_type: Type[T], obj: PersistedObject,
parsing_plan_for_children: Dict[str, ParsingPlan],
logger: Logger, options: Dict[str, Dict[str, Any]]) -> T:
# first use the base parser
# first = self._base_parser._parse_m... | Implementation of AnyParser API |
def are_worth_chaining(base_parser: Parser, to_type: Type[S], converter: Converter[S,T]) -> bool:
if isinstance(converter, ConversionChain):
for conv in converter._converters_list:
if not Parser.are_worth_chaining(base_parser, to_type, conv):
return False... | Utility method to check if it makes sense to chain this parser configured with the given to_type, with this
converter. It is an extension of ConverterChain.are_worth_chaining
:param base_parser:
:param to_type:
:param converter:
:return: |
def set_mode(self, mode):
_LOGGER.debug('State change called from alarm device')
if not mode:
_LOGGER.info('No mode supplied')
elif mode not in CONST.ALL_MODES:
_LOGGER.warning('Invalid mode')
response_object = self._lupusec.set_mode(CONST.MODE_TRANSLATIO... | Set Lupusec alarm mode. |
def parse_amendements_summary(url, json_response):
amendements = []
fields = [convert_camelcase_to_underscore(field) for field in
json_response['infoGenerales']['description_schema'].split('|')]
for row in json_response['data_table']:
values = row.split('|')
amd = Amende... | json schema :
{
infoGenerales: {
nb_resultats, debut, nb_docs
},
data_table: 'id|numInit|titreDossierLegislatif|urlDossierLegislatif|'
'instance|numAmend|urlAmend|designationArticle|'
'designationAlinea|dateDepot|signataires|sort'
}
NB : the jso... |
def get(self, **kwargs):
params = self.default_params.copy()
params.update(kwargs)
start = time.time()
response = requests.get(self.base_url, params=params)
end = time.time()
LOGGER.debug(
'fetched amendements with search params: %s in %0.2f s',
... | :param texteRecherche:
:param numAmend:
:param idArticle:
:param idAuteur:
:param idDossierLegislatif:
:param idExamen:
:param idExamens:
:param periodeParlementaire:
:param dateDebut:
:param dateFin:
:param rows:
:param start:
... |
def to_utf8(value):
if isinstance(value, unicode):
return value.encode('utf-8')
assert isinstance(value, str)
return value | Returns a string encoded using UTF-8.
This function comes from `Tornado`_.
:param value:
A unicode or string to be encoded.
:returns:
The encoded string. |
def to_unicode(value):
if isinstance(value, str):
return value.decode('utf-8')
assert isinstance(value, unicode)
return value | Returns a unicode string from a string, using UTF-8 to decode if needed.
This function comes from `Tornado`_.
:param value:
A unicode or string to be decoded.
:returns:
The decoded string. |
def find_all_commands(management_dir):
try:
#Find all commands in the directory that are not __init__.py and end in .py. Then, remove the trailing .py
return [f[:-3] for f in os.listdir(management_dir) if f.endswith('.py') and not f.startswith("__")]
except OSError:
#If nothing is ... | Find all valid commands in a directory
management_dir : directory path
return - List of commands |
def find_commands_module(app_name):
parts = app_name.split('.')
parts.append('commands')
parts.reverse()
part = parts.pop()
path = None
#Load the module if needed
try:
f, path, descr = imp.find_module(part, path)
except ImportError as e:
if os.path.basename(os.getcw... | Find the commands module in each app (if it exists) and return the path
app_name : The name of an app in the INSTALLED_APPS setting
return - path to the app |
def get_commands():
commands = {}
#Try to load the settings file (settings can be specified on the command line) and get the INSTALLED_APPS
try:
from percept.conf.base import settings
apps = settings.INSTALLED_APPS
except KeyError:
apps = []
#For each app, try to find ... | Get all valid commands
return - all valid commands in dictionary form |
def execute(self):
#Initialize the option parser
parser = LaxOptionParser(
usage="%prog subcommand [options] [args]",
option_list=BaseCommand.option_list #This will define what is allowed input to the parser (ie --settings=)
)
#Parse the options
... | Run the command with the command line arguments |
def help_text(self):
help_text = '\n'.join(sorted(get_commands().keys()))
help_text = "\nCommands:\n" + help_text
return help_text | Formats and prints the help text from the command list |
def missing(self, field, last=True):
'''
Numeric fields support specific handling for missing fields in a doc.
The missing value can be _last, _first, or a custom value
(that will be used for missing docs as the sort value).
missing('price')
> {"price" : {"missing": "_la... | Numeric fields support specific handling for missing fields in a doc.
The missing value can be _last, _first, or a custom value
(that will be used for missing docs as the sort value).
missing('price')
> {"price" : {"missing": "_last" } }
missing('price',False)
> {"price"... |
def ensure_table(self, cls):
cur = self._conn().cursor()
table_name = cls.get_table_name()
index_names = cls.index_names() or []
cols = ['id text primary key', 'value text']
for name in index_names:
cols.append(name + ' text')
cur.execute('create t... | Ensure table's existence - as per the gludb spec. |
def find_by_index(self, cls, index_name, value):
cur = self._conn().cursor()
query = 'select id,value from %s where %s = ?' % (
cls.get_table_name(),
index_name
)
found = []
for row in cur.execute(query, (value,)):
id, data = row[0],... | Find all rows matching index query - as per the gludb spec. |
def delete(self, obj):
del_id = obj.get_id()
if not del_id:
return
cur = self._conn().cursor()
tabname = obj.__class__.get_table_name()
query = 'delete from %s where id = ?' % tabname
cur.execute(query, (del_id,))
self._conn().commit()
... | Required functionality. |
def register(self):
register_url = self.base_url + "api/0.1.0/register"
register_headers = {
"apikey": str(self.owner_api_key),
"resourceID": str(self.entity_id),
"serviceType": "publish,subscribe,historicData"
}
with self.no_ssl_verification(... | Registers a new device with the name entity_id. This device has permissions for services like subscribe,
publish and access historical data. |
def no_ssl_verification(self):
try:
from functools import partialmethod
except ImportError:
# Python 2 fallback: https://gist.github.com/carymrobbins/8940382
from functools import partial
class partialmethod(partial):
def __get__(... | Requests module fails due to lets encrypt ssl encryption. Will be fixed in the future release. |
def publish(self, data):
if self.entity_api_key == "":
return {'status': 'failure', 'response': 'No API key found in request'}
publish_url = self.base_url + "api/0.1.0/publish"
publish_headers = {"apikey": self.entity_api_key}
publish_data = {
"exchange":... | This function allows an entity to publish data to the middleware.
Args:
data (string): contents to be published by this entity. |
def db(self, entity, query_filters="size=10"):
if self.entity_api_key == "":
return {'status': 'failure', 'response': 'No API key found in request'}
historic_url = self.base_url + "api/0.1.0/historicData?" + query_filters
historic_headers = {
"apikey": self.enti... | This function allows an entity to access the historic data.
Args:
entity (string): Name of the device to listen to
query_filters (string): Elastic search response format string
example, "pretty=true&size=10" |
def bind(self, devices_to_bind):
if self.entity_api_key == "":
return {'status': 'failure', 'response': 'No API key found in request'}
url = self.base_url + "api/0.1.0/subscribe/bind"
headers = {"apikey": self.entity_api_key}
data = {
"exchange": "amq.top... | This function allows an entity to list the devices to subscribe for data. This function must be called
at least once, before doing a subscribe. Subscribe function will listen to devices that are bound here.
Args:
devices_to_bind (list): an array of devices to listen to.
... |
def unbind(self, devices_to_unbind):
if self.entity_api_key == "":
return {'status': 'failure', 'response': 'No API key found in request'}
url = self.base_url + "api/0.1.0/subscribe/unbind"
headers = {"apikey": self.entity_api_key}
data = {
"exchange": "a... | This function allows an entity to unbound devices that are already bound.
Args:
devices_to_unbind (list): an array of devices that are to be unbound ( stop listening)
Example unbind(["test10","testDemo105"]) |
def subscribe(self, devices_to_bind=[]):
if self.entity_api_key == "":
return {'status': 'failure', 'response': 'No API key found in request'}
self.bind(devices_to_bind)
loop = asyncio.new_event_loop()
t1 = threading.Thread(target=self.start_subscribe_worker, args=(l... | This function allows an entity to subscribe for data from the devices specified in the bind operation. It
creates a thread with an event loop to manager the tasks created in start_subscribe_worker.
Args:
devices_to_bind (list): an array of devices to listen to |
def start_subscribe_worker(self, loop):
url = self.base_url + "api/0.1.0/subscribe"
task = loop.create_task(self.asynchronously_get_data(url + "?name={0}".format(self.entity_id)))
asyncio.set_event_loop(loop)
loop.run_until_complete(task)
self.event_loop = loop | Switch to new event loop as a thread and run until complete. |
async def asynchronously_get_data(self, url):
headers = {"apikey": self.entity_api_key}
try:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
async with session.get(url, headers=headers, timeout=3000) as response:
... | Asynchronously get data from Chunked transfer encoding of https://smartcity.rbccps.org/api/0.1.0/subscribe.
(Only this function requires Python 3. Rest of the functions can be run in python2.
Args:
url (string): url to subscribe |
def stop_subscribe(self):
asyncio.gather(*asyncio.Task.all_tasks()).cancel()
self.event_loop.stop()
self.event_loop.close() | This function is used to stop the event loop created when subscribe is called. But this function doesn't
stop the thread and should be avoided until its completely developed. |
def timeuntil(d, now=None):
if not now:
if getattr(d, 'tzinfo', None):
now = datetime.datetime.now(LocalTimezone(d))
else:
now = datetime.datetime.now()
return timesince(now, d) | Like timesince, but returns a string measuring the time until
the given time. |
def updateCache(self, service, url, new_data, new_data_dt):
key = self._get_key(service, url)
# clear existing data
try:
value = self.client.get(key)
if value:
data = pickle.loads(value, encoding="utf8")
if "time_stamp" in data:
... | :param new_data: a string representation of the data
:param new_data_dt: a timezone aware datetime object giving
the timestamp of the new_data
:raise MemcachedException: if update failed |
def delete_all_eggs(self):
path_to_delete = os.path.join(self.egg_directory, "lib", "python")
if os.path.exists(path_to_delete):
shutil.rmtree(path_to_delete) | delete all the eggs in the directory specified |
def install_egg(self, egg_name):
if not os.path.exists(self.egg_directory):
os.makedirs(self.egg_directory)
self.requirement_set.add_requirement(
InstallRequirement.from_line(egg_name, None))
try:
self.requirement_set.prepare_files(self.finder)
... | Install an egg into the egg directory |
def call(cls, iterable, *a, **kw):
return cls(x(*a, **kw) for x in iterable) | Calls every item in *iterable* with the specified arguments. |
def map(cls, iterable, func, *a, **kw):
return cls(func(x, *a, **kw) for x in iterable) | Iterable-first replacement of Python's built-in `map()` function. |
def filter(cls, iterable, cond, *a, **kw):
return cls(x for x in iterable if cond(x, *a, **kw)) | Iterable-first replacement of Python's built-in `filter()` function. |
def unique(cls, iterable, key=None):
if key is None:
key = lambda x: x
def generator():
seen = set()
seen_add = seen.add
for item in iterable:
key_val = key(item)
if key_val not in seen:
seen_add(key_val)
yield item
return cls(generator()) | Yields unique items from *iterable* whilst preserving the original order. |
def chunks(cls, iterable, n, fill=None):
return cls(itertools.zip_longest(*[iter(iterable)] * n, fillvalue=fill)) | Collects elements in fixed-length chunks. |
def concat(cls, iterables):
def generator():
for it in iterables:
for element in it:
yield element
return cls(generator()) | Similar to #itertools.chain.from_iterable(). |
def chain(cls, *iterables):
def generator():
for it in iterables:
for element in it:
yield element
return cls(generator()) | Similar to #itertools.chain.from_iterable(). |
def attr(cls, iterable, attr_name):
return cls(getattr(x, attr_name) for x in iterable) | Applies #getattr() on all elements of *iterable*. |
def of_type(cls, iterable, types):
return cls(x for x in iterable if isinstance(x, types)) | Filters using #isinstance(). |
def partition(cls, iterable, pred):
t1, t2 = itertools.tee(iterable)
return cls(itertools.filterfalse(pred, t1), filter(pred, t2)) | Use a predicate to partition items into false and true entries. |
def count(cls, iterable):
iterable = iter(iterable)
count = 0
while True:
try:
next(iterable)
except StopIteration:
break
count += 1
return count | Returns the number of items in an iterable. |
def column_names(self, table):
table_info = self.execute(
u'PRAGMA table_info(%s)' % quote(table))
return (column['name'] for column in table_info) | An iterable of column names, for a particular table or
view. |
def execute(self, sql, *args, **kwargs):
'''
Run raw SQL on the database, and receive relaxing output.
This is sort of the foundational method that most of the
others build on.
'''
try:
self.cursor.execute(sql, *args)
except self.sqlite3.InterfaceError, msg:
raise self.sqlite3.In... | Run raw SQL on the database, and receive relaxing output.
This is sort of the foundational method that most of the
others build on. |
def create_index(self, columns, table_name, if_not_exists = True, unique = False, **kwargs):
'Create a unique index on the column(s) passed.'
index_name = simplify(table_name) + u'_' + u'_'.join(map(simplify, columns))
if unique:
sql = u'CREATE UNIQUE INDEX %s ON %s (%s)'
else:
sql = u'CREAT... | Create a unique index on the column(s) passed. |
def get_var(self, key):
'Retrieve one saved variable from the database.'
vt = quote(self.__vars_table)
data = self.execute(u'SELECT * FROM %s WHERE `key` = ?' % vt, [key], commit = False)
if data == []:
raise NameError(u'The DumpTruck variables table doesn\'t have a value for %s.' % key)
else:... | Retrieve one saved variable from the database. |
def tablesAndViews(self):
result = self.execute(
u'SELECT name,type FROM sqlite_master WHERE type in ("table", "view")',
commit=False)
return ((row['name'],row['type']) for row in result) | Return a sequence of (name,type) pairs where type is
either "table" or "view". |
def drop(self, table_name = 'dumptruck', if_exists = False, **kwargs):
'Drop a table.'
return self.execute(u'DROP TABLE %s %s;' % ('IF EXISTS' if if_exists else '', quote(table_name)), **kwargsf drop(self, table_name = 'dumptruck', if_exists = False, **kwargs):
'Drop a table.'
return self.execute(u'DROP... | Drop a table. |
def __install_perforce(self, config):
if not system.is_64_bit():
self.logger.warn("Perforce formula is only designed for 64 bit systems! Not install executables...")
return False
version = config.get('version', 'r13.2')
key = 'osx' if system.is_osx() else 'linux'... | install perforce binary |
def _install_p4v_osx(self, url, overwrite=False):
package_exists = False
root_dir = os.path.expanduser(os.path.join("~", "Applications"))
package_exists = len([x for x in P4V_APPLICATIONS if os.path.exists(os.path.join(root_dir, x))])
if not package_exists or overwrite:
... | Install perforce applications and binaries for mac |
def _install_p4v_linux(self, url):
lib.extract_targz(url,
self.directory.install_directory(self.feature_name),
remove_common_prefix=True)
bin_path = os.path.join(self.directory.install_directory(self.feature_name), 'bin')
if os.path.ex... | Install perforce applications and binaries for linux |
def __write_p4settings(self, config):
self.logger.info("Writing p4settings...")
root_dir = os.path.expanduser(config.get('root_path'))
p4settings_path = os.path.join(root_dir, ".p4settings")
if os.path.exists(p4settings_path):
if self.target.get('overwrite_p4settings... | write perforce settings |
def __configure_client(self, config):
self.logger.info("Configuring p4 client...")
client_dict = config.to_dict()
client_dict['root_path'] = os.path.expanduser(config.get('root_path'))
os.chdir(client_dict['root_path'])
client_dict['hostname'] = system.NODE
clien... | write the perforce client |
def __install_eggs(self, config):
egg_carton = (self.directory.install_directory(self.feature_name),
'requirements.txt')
eggs = self.__gather_eggs(config)
self.logger.debug("Installing eggs %s..." % eggs)
self.__load_carton(egg_carton, eggs)
self.... | Install eggs for a particular configuration |
def __add_paths(self, config):
bin_path = os.path.join(self.directory.install_directory(self.feature_name), 'bin')
whitelist_executables = self._get_whitelisted_executables(config)
for f in os.listdir(bin_path):
for pattern in BLACKLISTED_EXECUTABLES:
if re.m... | add the proper resources into the environment |
def analyse_body_paragraph(body_paragraph, labels=None):
# try to find leading label first:
for label, dummy in labels:
if body_paragraph.startswith('* ' + label):
return (label, body_paragraph[len(label) + 3:].replace('\n ',
... | Analyse commit body paragraph and return (label, message).
>>> analyse_body_paragraph('* BETTER Foo and bar.',
>>> ... {'BETTER': 'Improvements'})
('BETTER', 'Foo and bar.')
>>> analyse_body_paragraph('* Foo and bar.')
(None, 'Foo and bar.')
>>> analyse_body_paragraph('Foo and bar.')
(None,... |
def remove_ticket_directives(message):
if message:
message = re.sub(r'closes #', '#', message)
message = re.sub(r'addresses #', '#', message)
message = re.sub(r'references #', '#', message)
return message | Remove ticket directives like "(closes #123).
>>> remove_ticket_directives('(closes #123)')
'(#123)'
>>> remove_ticket_directives('(foo #123)')
'(foo #123)' |
def amended_commits(commits):
# which SHA1 are declared as amended later?
amended_sha1s = []
for message in commits.values():
amended_sha1s.extend(re.findall(r'AMENDS\s([0-f]+)', message))
return amended_sha1s | Return those git commit sha1s that have been amended later. |
def enrich_git_log_dict(messages, labels):
for commit_sha1, message in messages.items():
# detect module and ticket numbers for each commit:
component = None
title = message.split('\n')[0]
try:
component, title = title.split(":", 1)
component = component.... | Enrich git log with related information on tickets. |
def release(obj, commit='HEAD', components=False):
options = obj.options
repository = obj.repository
try:
sha = 'oid'
commits = _pygit2_commits(commit, repository)
except ImportError:
try:
sha = 'hexsha'
commits = _git_commits(commit, repository)
... | Generate release notes. |
def incr(**vars):
for k, v in vars:
current_context.vars.setdefault(k, 0)
current_context[k] += v | Increments context variables |
def set_default_var(**vars):
for k, v in vars.iteritems():
current_context.vars.setdefault(k, v) | Sets context variables using the key/value provided in the options |
def incr_obj(obj, **attrs):
for name, value in attrs.iteritems():
v = getattr(obj, name, None)
if not hasattr(obj, name) or v is None:
v = 0
setattr(obj, name, v + value) | Increments context variables |
def redirect(view=None, url=None, **kwargs):
if view:
if url:
kwargs["url"] = url
url = flask.url_for(view, **kwargs)
current_context.exit(flask.redirect(url)) | Redirects to the specified view or url |
def lines(query):
filename = support.get_file_name(query)
if(os.path.isfile(filename)):
with open(filename) as openfile:
print len(openfile.readlines())
else:
print 'File not found : ' + filename | lines(query) -- print the number of lines in a given file |
def words(query):
filename = support.get_file_name(query)
if(os.path.isfile(filename)):
with open(filename) as openfile:
print len(openfile.read().split())
else:
print 'File not found : ' + filename | lines(query) -- print the number of words in a given file |
def file_info(query):
filename = support.get_file_name(query)
if(os.path.isfile(filename)):
stat_info = os.stat(filename)
owner_name = pwd.getpwuid(stat_info.st_uid).pw_name
print 'owner : ' + owner_name
file_size = support.get_readable_filesize(stat_info.st_size)
print 'size : ' + file_size
print 'crea... | file_info(query) -- print some human readable information of a given file |
def make_executable(query):
filename = support.get_file_name(query)
if(os.path.isfile(filename)):
os.system('chmod +x '+filename)
else:
print 'file not found' | make_executable(query) -- give executable permissions to a given file |
def add_to_path(query):
new_entry = support.get_path(query)
if(new_entry):
print 'Adding '+new_entry+' to PATH variable.'
print '''1 : confirm
2 : cancel
'''
choice = int(raw_input('>> '))
if(choice == 1):
home_dir = os.path.expanduser('~')
bashrc = open(os.path.join(home_dir, ".bashrc"), "a")
... | add_to_path(query) -- add user given path to environment PATH variable. |
def system_info(query):
proc = subprocess.Popen(["uname -o"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "operating system : "+str(out),
proc = subprocess.Popen(["uname"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "kernel : "+str(out),
proc = sub... | system_info(query) -- print system specific information like OS, kernel,
architecture etc. |
def statsd_metric(name, count, elapsed):
with statsd.pipeline() as pipe:
pipe.incr(name, count)
pipe.timing(name, int(round(1000 * elapsed))) | Metric that records to statsd & graphite |
def setup(template_paths={}, autoescape=False, cache_size=100, auto_reload=True, bytecode_cache=True):
global _jinja_env, _jinja_loaders
if not _jinja_env:
_jinja_env = JinjaEnviroment(
autoescape=autoescape,
cache_size=cache_size,
auto_reload=auto_reload,
bytecode_cache=None)
#... | Setup Jinja enviroment
eg. sketch.jinja.setup({
'app': self.config.paths['app_template_basedir'],
'sketch': self.config.paths['sketch_template_dir'],
})
:param template_paths: Dictionary of paths to templates (template_name => template_path)
:param autoescape: Autoescape
:param cache_size:... |
def render(template_name, template_vars={}, template_set='site', template_theme=None, template_extension='html', template_content=None):
global _jinja_env
if not _jinja_env:
raise 'Jinja env not setup'
try:
_jinja_env.filters['timesince'] = timesince
_jinja_env.filters['timeuntil'] = timeuntil
... | Given a template path, a template name and template variables
will return rendered content using jinja2 library
:param template_path: Path to template directory
:param template_name: Name of template
:param vars: (Optional) Template variables |
def compile_file(env, src_path, dst_path, encoding='utf-8', base_dir=''):
src_file = file(src_path, 'r')
source = src_file.read().decode(encoding)
name = src_path.replace(base_dir, '')
raw = env.compile(source, name=name, filename=name, raw=True)
src_file.close()
dst_file = open(dst_path, 'w')
dst_fil... | Compiles a Jinja2 template to python code.
:param env: a Jinja2 Environment instance.
:param src_path: path to the source file.
:param dst_path: path to the destination file.
:param encoding: template encoding.
:param base_dir: the base path to be removed from the compiled template filename. |
def compile_dir(env, src_path, dst_path, pattern=r'^.*\.html$', encoding='utf-8', base_dir=None):
from os import path, listdir, mkdir
file_re = re.compile(pattern)
if base_dir is None:
base_dir = src_path
for filename in listdir(src_path):
src_name = path.join(src_path, filename)
dst_name = pat... | Compiles a directory of Jinja2 templates to python code.
:param env: a Jinja2 Environment instance.
:param src_path: path to the source directory.
:param dst_path: path to the destination directory.
:param encoding: template encoding.
:param base_dir: the base path to be removed from the compiled template ... |
def _pre_dump(cls):
shutil.rmtree(cls.outdir, ignore_errors=True)
os.makedirs(cls.outdir)
super(PlotMetric, cls)._pre_dump() | Output all recorded stats |
def _histogram(self, which, mu, sigma, data):
weights = np.ones_like(data)/len(data) # make bar heights sum to 100%
n, bins, patches = plt.hist(data, bins=25, weights=weights, facecolor='blue', alpha=0.5)
plt.title(r'%s %s: $\mu=%.2f$, $\sigma=%.2f$' % (self.name, which.capitalize(), ... | plot a histogram. For internal use only |
def _scatter(self):
plt.scatter(self.count_arr, self.elapsed_arr)
plt.title('{}: Count vs. Elapsed'.format(self.name))
plt.xlabel('Items')
plt.ylabel('Seconds') | plot a scatter plot of count vs. elapsed. For internal use only |
def bind(self, __fun, *args, **kwargs):
with self._lock:
if self._running or self._completed or self._cancelled:
raise RuntimeError('Future object can not be reused')
if self._worker:
raise RuntimeError('Future object is already bound')
self._worker = functools.partial(__fun,... | Bind a worker function to the future. This worker function will be
executed when the future is executed. |
def add_done_callback(self, fun):
with self._lock:
if self._completed:
fun()
else:
self._done_callbacks.append(fun) | Adds the callback *fun* to the future so that it be invoked when the
future completed. The future completes either when it has been completed
after being started with the :meth:`start` method (independent of whether
an error occurs or not) or when either :meth:`set_result` or
:meth:`set_exception` is ca... |
def enqueue(self):
with self._lock:
if self._enqueued:
raise RuntimeError('Future object is already enqueued')
if self._running:
raise RuntimeError('Future object is already running')
if self._completed:
raise RuntimeError('Future object can not be restarted')
i... | Mark the future as being enqueued in some kind of executor for futures.
Calling :meth:`start()` with the *as_thread* parameter as :const:`True`
will raise a :class:`RuntimeError` after this method has been called.
This method will also validate the state of the future. |
def start(self, as_thread=True):
with self._lock:
if as_thread:
self.enqueue() # Validate future state
if self._cancelled:
return
self._running = True
if as_thread:
self._thread = threading.Thread(target=self._run)
self._thread.start()
return s... | Execute the future in a new thread or in the current thread as specified
by the *as_thread* parameter.
:param as_thread: Execute the future in a new, separate thread. If this
is set to :const:`False`, the future will be executed in the calling
thread. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.