partition
stringclasses
3 values
func_name
stringlengths
1
134
docstring
stringlengths
1
46.9k
path
stringlengths
4
223
original_string
stringlengths
75
104k
code
stringlengths
75
104k
docstring_tokens
listlengths
1
1.97k
repo
stringlengths
7
55
language
stringclasses
1 value
url
stringlengths
87
315
code_tokens
listlengths
19
28.4k
sha
stringlengths
40
40
test
Table.chart_plot
Plot a nuclear chart with (N,Z) as axis and the values of the Table as a color scale Parameters ---------- ax: optional matplotlib axes defaults to current axes cmap: a matplotlib colormap default: 'RdBu' xlabel: string representing the label of the x axis default: 'N' ylabel: string, default: 'Z' the label of the x axis grid_on: boolean, default: True, whether to draw the axes grid or not colorbar: boolean, default: True whether to draw a colorbar or not Returns ------- ax: a matplotlib axes object Example ------- Plot the theoretical deviation for the Möller's model:: >>> Table('FRDM95').error().chart_plot()
masstable/masstable.py
def chart_plot(self, ax=None, cmap='RdBu', xlabel='N', ylabel='Z', grid_on=True, colorbar=True): """Plot a nuclear chart with (N,Z) as axis and the values of the Table as a color scale Parameters ---------- ax: optional matplotlib axes defaults to current axes cmap: a matplotlib colormap default: 'RdBu' xlabel: string representing the label of the x axis default: 'N' ylabel: string, default: 'Z' the label of the x axis grid_on: boolean, default: True, whether to draw the axes grid or not colorbar: boolean, default: True whether to draw a colorbar or not Returns ------- ax: a matplotlib axes object Example ------- Plot the theoretical deviation for the Möller's model:: >>> Table('FRDM95').error().chart_plot() """ from matplotlib.mlab import griddata from numpy import linspace, meshgrid import matplotlib.pyplot as plt # extract the 1D arrays to be plotted x = self.dropna().N y = self.dropna().Z z = self.dropna().values #convert to matplotlibs grid format xi = linspace(min(x), max(x), max(x) - min(x) + 1) yi = linspace(min(y), max(y), max(y) - min(y) + 1) Z = griddata(x, y, z, xi, yi) X, Y = meshgrid(xi, yi) # create and customize plot if ax is None: ax = plt.gca() chart = ax.pcolormesh(X, Y, Z, cmap=cmap) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.grid(grid_on) ax.set_aspect('equal') if colorbar: plt.colorbar(chart) return ax
def chart_plot(self, ax=None, cmap='RdBu', xlabel='N', ylabel='Z', grid_on=True, colorbar=True): """Plot a nuclear chart with (N,Z) as axis and the values of the Table as a color scale Parameters ---------- ax: optional matplotlib axes defaults to current axes cmap: a matplotlib colormap default: 'RdBu' xlabel: string representing the label of the x axis default: 'N' ylabel: string, default: 'Z' the label of the x axis grid_on: boolean, default: True, whether to draw the axes grid or not colorbar: boolean, default: True whether to draw a colorbar or not Returns ------- ax: a matplotlib axes object Example ------- Plot the theoretical deviation for the Möller's model:: >>> Table('FRDM95').error().chart_plot() """ from matplotlib.mlab import griddata from numpy import linspace, meshgrid import matplotlib.pyplot as plt # extract the 1D arrays to be plotted x = self.dropna().N y = self.dropna().Z z = self.dropna().values #convert to matplotlibs grid format xi = linspace(min(x), max(x), max(x) - min(x) + 1) yi = linspace(min(y), max(y), max(y) - min(y) + 1) Z = griddata(x, y, z, xi, yi) X, Y = meshgrid(xi, yi) # create and customize plot if ax is None: ax = plt.gca() chart = ax.pcolormesh(X, Y, Z, cmap=cmap) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.grid(grid_on) ax.set_aspect('equal') if colorbar: plt.colorbar(chart) return ax
[ "Plot", "a", "nuclear", "chart", "with", "(", "N", "Z", ")", "as", "axis", "and", "the", "values", "of", "the", "Table", "as", "a", "color", "scale" ]
elyase/masstable
python
https://github.com/elyase/masstable/blob/3eb72b22cd3337bc5c6bb95bb7bb73fdbe6ae9e2/masstable/masstable.py#L519-L576
[ "def", "chart_plot", "(", "self", ",", "ax", "=", "None", ",", "cmap", "=", "'RdBu'", ",", "xlabel", "=", "'N'", ",", "ylabel", "=", "'Z'", ",", "grid_on", "=", "True", ",", "colorbar", "=", "True", ")", ":", "from", "matplotlib", ".", "mlab", "import", "griddata", "from", "numpy", "import", "linspace", ",", "meshgrid", "import", "matplotlib", ".", "pyplot", "as", "plt", "# extract the 1D arrays to be plotted", "x", "=", "self", ".", "dropna", "(", ")", ".", "N", "y", "=", "self", ".", "dropna", "(", ")", ".", "Z", "z", "=", "self", ".", "dropna", "(", ")", ".", "values", "#convert to matplotlibs grid format", "xi", "=", "linspace", "(", "min", "(", "x", ")", ",", "max", "(", "x", ")", ",", "max", "(", "x", ")", "-", "min", "(", "x", ")", "+", "1", ")", "yi", "=", "linspace", "(", "min", "(", "y", ")", ",", "max", "(", "y", ")", ",", "max", "(", "y", ")", "-", "min", "(", "y", ")", "+", "1", ")", "Z", "=", "griddata", "(", "x", ",", "y", ",", "z", ",", "xi", ",", "yi", ")", "X", ",", "Y", "=", "meshgrid", "(", "xi", ",", "yi", ")", "# create and customize plot", "if", "ax", "is", "None", ":", "ax", "=", "plt", ".", "gca", "(", ")", "chart", "=", "ax", ".", "pcolormesh", "(", "X", ",", "Y", ",", "Z", ",", "cmap", "=", "cmap", ")", "ax", ".", "set_xlabel", "(", "xlabel", ")", "ax", ".", "set_ylabel", "(", "ylabel", ")", "ax", ".", "grid", "(", "grid_on", ")", "ax", ".", "set_aspect", "(", "'equal'", ")", "if", "colorbar", ":", "plt", ".", "colorbar", "(", "chart", ")", "return", "ax" ]
3eb72b22cd3337bc5c6bb95bb7bb73fdbe6ae9e2
test
_uses_db
Use as a decorator for operations on the database, to ensure connection setup and teardown. Can only be used on methods on objects with a `self.session` attribute.
pwm/core.py
def _uses_db(func, self, *args, **kwargs): """ Use as a decorator for operations on the database, to ensure connection setup and teardown. Can only be used on methods on objects with a `self.session` attribute. """ if not self.session: _logger.debug('Creating new db session') self._init_db_session() try: ret = func(self, *args, **kwargs) self.session.commit() except: self.session.rollback() tb = traceback.format_exc() _logger.debug(tb) raise finally: _logger.debug('Closing db session') self.session.close() return ret
def _uses_db(func, self, *args, **kwargs): """ Use as a decorator for operations on the database, to ensure connection setup and teardown. Can only be used on methods on objects with a `self.session` attribute. """ if not self.session: _logger.debug('Creating new db session') self._init_db_session() try: ret = func(self, *args, **kwargs) self.session.commit() except: self.session.rollback() tb = traceback.format_exc() _logger.debug(tb) raise finally: _logger.debug('Closing db session') self.session.close() return ret
[ "Use", "as", "a", "decorator", "for", "operations", "on", "the", "database", "to", "ensure", "connection", "setup", "and", "teardown", ".", "Can", "only", "be", "used", "on", "methods", "on", "objects", "with", "a", "self", ".", "session", "attribute", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/core.py#L112-L130
[ "def", "_uses_db", "(", "func", ",", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "not", "self", ".", "session", ":", "_logger", ".", "debug", "(", "'Creating new db session'", ")", "self", ".", "_init_db_session", "(", ")", "try", ":", "ret", "=", "func", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", "self", ".", "session", ".", "commit", "(", ")", "except", ":", "self", ".", "session", ".", "rollback", "(", ")", "tb", "=", "traceback", ".", "format_exc", "(", ")", "_logger", ".", "debug", "(", "tb", ")", "raise", "finally", ":", "_logger", ".", "debug", "(", "'Closing db session'", ")", "self", ".", "session", ".", "close", "(", ")", "return", "ret" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
Domain.derive_key
Computes the key from the salt and the master password.
pwm/core.py
def derive_key(self, master_password): """ Computes the key from the salt and the master password. """ encoder = encoding.Encoder(self.charset) bytes = ('%s:%s' % (master_password, self.name)).encode('utf8') start_time = time.clock() # we fix the scrypt parameters in case the defaults change digest = scrypt.hash(bytes, self.salt, N=1<<14, r=8, p=1) key = encoder.encode(digest, self.key_length) derivation_time_in_s = time.clock() - start_time _logger.debug('Key derivation took %.2fms', derivation_time_in_s*1000) return key
def derive_key(self, master_password): """ Computes the key from the salt and the master password. """ encoder = encoding.Encoder(self.charset) bytes = ('%s:%s' % (master_password, self.name)).encode('utf8') start_time = time.clock() # we fix the scrypt parameters in case the defaults change digest = scrypt.hash(bytes, self.salt, N=1<<14, r=8, p=1) key = encoder.encode(digest, self.key_length) derivation_time_in_s = time.clock() - start_time _logger.debug('Key derivation took %.2fms', derivation_time_in_s*1000) return key
[ "Computes", "the", "key", "from", "the", "salt", "and", "the", "master", "password", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/core.py#L67-L81
[ "def", "derive_key", "(", "self", ",", "master_password", ")", ":", "encoder", "=", "encoding", ".", "Encoder", "(", "self", ".", "charset", ")", "bytes", "=", "(", "'%s:%s'", "%", "(", "master_password", ",", "self", ".", "name", ")", ")", ".", "encode", "(", "'utf8'", ")", "start_time", "=", "time", ".", "clock", "(", ")", "# we fix the scrypt parameters in case the defaults change", "digest", "=", "scrypt", ".", "hash", "(", "bytes", ",", "self", ".", "salt", ",", "N", "=", "1", "<<", "14", ",", "r", "=", "8", ",", "p", "=", "1", ")", "key", "=", "encoder", ".", "encode", "(", "digest", ",", "self", ".", "key_length", ")", "derivation_time_in_s", "=", "time", ".", "clock", "(", ")", "-", "start_time", "_logger", ".", "debug", "(", "'Key derivation took %.2fms'", ",", "derivation_time_in_s", "*", "1000", ")", "return", "key" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
PWM.bootstrap
Initialize a database. :param database_path: The absolute path to the database to initialize.
pwm/core.py
def bootstrap(self, path_or_uri): """ Initialize a database. :param database_path: The absolute path to the database to initialize. """ _logger.debug("Bootstrapping new database: %s", path_or_uri) self.database_uri = _urify_db(path_or_uri) db = sa.create_engine(self.database_uri) Base.metadata.create_all(db)
def bootstrap(self, path_or_uri): """ Initialize a database. :param database_path: The absolute path to the database to initialize. """ _logger.debug("Bootstrapping new database: %s", path_or_uri) self.database_uri = _urify_db(path_or_uri) db = sa.create_engine(self.database_uri) Base.metadata.create_all(db)
[ "Initialize", "a", "database", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/core.py#L147-L155
[ "def", "bootstrap", "(", "self", ",", "path_or_uri", ")", ":", "_logger", ".", "debug", "(", "\"Bootstrapping new database: %s\"", ",", "path_or_uri", ")", "self", ".", "database_uri", "=", "_urify_db", "(", "path_or_uri", ")", "db", "=", "sa", ".", "create_engine", "(", "self", ".", "database_uri", ")", "Base", ".", "metadata", ".", "create_all", "(", "db", ")" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
PWM.search
Search the database for the given query. Will find partial matches.
pwm/core.py
def search(self, query): """ Search the database for the given query. Will find partial matches. """ results = self.session.query(Domain).filter(Domain.name.ilike('%%%s%%' % query)).all() return results
def search(self, query): """ Search the database for the given query. Will find partial matches. """ results = self.session.query(Domain).filter(Domain.name.ilike('%%%s%%' % query)).all() return results
[ "Search", "the", "database", "for", "the", "given", "query", ".", "Will", "find", "partial", "matches", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/core.py#L159-L162
[ "def", "search", "(", "self", ",", "query", ")", ":", "results", "=", "self", ".", "session", ".", "query", "(", "Domain", ")", ".", "filter", "(", "Domain", ".", "name", ".", "ilike", "(", "'%%%s%%'", "%", "query", ")", ")", ".", "all", "(", ")", "return", "results" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
PWM.get_domain
Get the :class:`Domain <pwm.Domain>` object from a name. :param domain_name: The domain name to fetch the object for. :returns: The :class:`Domain <pwm.core.Domain>` class with this domain_name if found, else None.
pwm/core.py
def get_domain(self, domain_name): """ Get the :class:`Domain <pwm.Domain>` object from a name. :param domain_name: The domain name to fetch the object for. :returns: The :class:`Domain <pwm.core.Domain>` class with this domain_name if found, else None. """ protocol = self.database_uri.split(':', 1)[0] if protocol in ('https', 'http'): return self._get_domain_from_rest_api(domain_name) else: domain = self._get_domain_from_db(domain_name) if domain: return domain else: raise NoSuchDomainException
def get_domain(self, domain_name): """ Get the :class:`Domain <pwm.Domain>` object from a name. :param domain_name: The domain name to fetch the object for. :returns: The :class:`Domain <pwm.core.Domain>` class with this domain_name if found, else None. """ protocol = self.database_uri.split(':', 1)[0] if protocol in ('https', 'http'): return self._get_domain_from_rest_api(domain_name) else: domain = self._get_domain_from_db(domain_name) if domain: return domain else: raise NoSuchDomainException
[ "Get", "the", ":", "class", ":", "Domain", "<pwm", ".", "Domain", ">", "object", "from", "a", "name", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/core.py#L166-L181
[ "def", "get_domain", "(", "self", ",", "domain_name", ")", ":", "protocol", "=", "self", ".", "database_uri", ".", "split", "(", "':'", ",", "1", ")", "[", "0", "]", "if", "protocol", "in", "(", "'https'", ",", "'http'", ")", ":", "return", "self", ".", "_get_domain_from_rest_api", "(", "domain_name", ")", "else", ":", "domain", "=", "self", ".", "_get_domain_from_db", "(", "domain_name", ")", "if", "domain", ":", "return", "domain", "else", ":", "raise", "NoSuchDomainException" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
PWM.modify_domain
Modify an existing domain. :param domain_name: The name of the domain to modify. :param new_salt: Whether to generate a new salt for the domain. :param username: If given, change domain username to this value. :returns: The modified :class:`Domain <pwm.core.Domain>` object.
pwm/core.py
def modify_domain(self, domain_name, new_salt=False, username=None): """ Modify an existing domain. :param domain_name: The name of the domain to modify. :param new_salt: Whether to generate a new salt for the domain. :param username: If given, change domain username to this value. :returns: The modified :class:`Domain <pwm.core.Domain>` object. """ domain = self._get_domain_from_db(domain_name) if domain is None: raise NoSuchDomainException if new_salt: _logger.info("Generating new salt..") domain.new_salt() if username is not None: domain.username = username return domain
def modify_domain(self, domain_name, new_salt=False, username=None): """ Modify an existing domain. :param domain_name: The name of the domain to modify. :param new_salt: Whether to generate a new salt for the domain. :param username: If given, change domain username to this value. :returns: The modified :class:`Domain <pwm.core.Domain>` object. """ domain = self._get_domain_from_db(domain_name) if domain is None: raise NoSuchDomainException if new_salt: _logger.info("Generating new salt..") domain.new_salt() if username is not None: domain.username = username return domain
[ "Modify", "an", "existing", "domain", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/core.py#L217-L233
[ "def", "modify_domain", "(", "self", ",", "domain_name", ",", "new_salt", "=", "False", ",", "username", "=", "None", ")", ":", "domain", "=", "self", ".", "_get_domain_from_db", "(", "domain_name", ")", "if", "domain", "is", "None", ":", "raise", "NoSuchDomainException", "if", "new_salt", ":", "_logger", ".", "info", "(", "\"Generating new salt..\"", ")", "domain", ".", "new_salt", "(", ")", "if", "username", "is", "not", "None", ":", "domain", ".", "username", "=", "username", "return", "domain" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
PWM.create_domain
Create a new domain entry in the database. :param username: The username to associate with this domain. :param alphabet: A character set restriction to impose on keys generated for this domain. :param length: The length of the generated key, in case of restrictions on the site.
pwm/core.py
def create_domain(self, domain_name, username=None, alphabet=Domain.DEFAULT_ALPHABET, length=Domain.DEFAULT_KEY_LENGTH): """ Create a new domain entry in the database. :param username: The username to associate with this domain. :param alphabet: A character set restriction to impose on keys generated for this domain. :param length: The length of the generated key, in case of restrictions on the site. """ # Wrap the actual implementation to do some error handling try: return self._create_domain(domain_name, username, alphabet, length) except Exception as ex: _logger.warn("Inserting new domain failed: %s", ex) raise DuplicateDomainException
def create_domain(self, domain_name, username=None, alphabet=Domain.DEFAULT_ALPHABET, length=Domain.DEFAULT_KEY_LENGTH): """ Create a new domain entry in the database. :param username: The username to associate with this domain. :param alphabet: A character set restriction to impose on keys generated for this domain. :param length: The length of the generated key, in case of restrictions on the site. """ # Wrap the actual implementation to do some error handling try: return self._create_domain(domain_name, username, alphabet, length) except Exception as ex: _logger.warn("Inserting new domain failed: %s", ex) raise DuplicateDomainException
[ "Create", "a", "new", "domain", "entry", "in", "the", "database", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/core.py#L236-L249
[ "def", "create_domain", "(", "self", ",", "domain_name", ",", "username", "=", "None", ",", "alphabet", "=", "Domain", ".", "DEFAULT_ALPHABET", ",", "length", "=", "Domain", ".", "DEFAULT_KEY_LENGTH", ")", ":", "# Wrap the actual implementation to do some error handling", "try", ":", "return", "self", ".", "_create_domain", "(", "domain_name", ",", "username", ",", "alphabet", ",", "length", ")", "except", "Exception", "as", "ex", ":", "_logger", ".", "warn", "(", "\"Inserting new domain failed: %s\"", ",", "ex", ")", "raise", "DuplicateDomainException" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
from_bbox
Yields tile (x, y, z) tuples for a bounding box and zoom levels. Arguments: bbox - bounding box as a 4-length sequence zlevs - sequence of tile zoom levels
greenwich/tile.py
def from_bbox(bbox, zlevs): """Yields tile (x, y, z) tuples for a bounding box and zoom levels. Arguments: bbox - bounding box as a 4-length sequence zlevs - sequence of tile zoom levels """ env = Envelope(bbox) for z in zlevs: corners = [to_tile(*coord + (z,)) for coord in (env.ul, env.lr)] xs, ys = [range(p1, p2 + 1) for p1, p2 in zip(*corners)] for coord in itertools.product(xs, ys, (z,)): yield coord
def from_bbox(bbox, zlevs): """Yields tile (x, y, z) tuples for a bounding box and zoom levels. Arguments: bbox - bounding box as a 4-length sequence zlevs - sequence of tile zoom levels """ env = Envelope(bbox) for z in zlevs: corners = [to_tile(*coord + (z,)) for coord in (env.ul, env.lr)] xs, ys = [range(p1, p2 + 1) for p1, p2 in zip(*corners)] for coord in itertools.product(xs, ys, (z,)): yield coord
[ "Yields", "tile", "(", "x", "y", "z", ")", "tuples", "for", "a", "bounding", "box", "and", "zoom", "levels", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/tile.py#L6-L18
[ "def", "from_bbox", "(", "bbox", ",", "zlevs", ")", ":", "env", "=", "Envelope", "(", "bbox", ")", "for", "z", "in", "zlevs", ":", "corners", "=", "[", "to_tile", "(", "*", "coord", "+", "(", "z", ",", ")", ")", "for", "coord", "in", "(", "env", ".", "ul", ",", "env", ".", "lr", ")", "]", "xs", ",", "ys", "=", "[", "range", "(", "p1", ",", "p2", "+", "1", ")", "for", "p1", ",", "p2", "in", "zip", "(", "*", "corners", ")", "]", "for", "coord", "in", "itertools", ".", "product", "(", "xs", ",", "ys", ",", "(", "z", ",", ")", ")", ":", "yield", "coord" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
to_lonlat
Returns a tuple of (longitude, latitude) from a map tile xyz coordinate. See http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_2 Arguments: xtile - x tile location as int or float ytile - y tile location as int or float zoom - zoom level as int or float
greenwich/tile.py
def to_lonlat(xtile, ytile, zoom): """Returns a tuple of (longitude, latitude) from a map tile xyz coordinate. See http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_2 Arguments: xtile - x tile location as int or float ytile - y tile location as int or float zoom - zoom level as int or float """ n = 2.0 ** zoom lon = xtile / n * 360.0 - 180.0 # Caculate latitude in radians and convert to degrees constrained from -90 # to 90. Values too big for tile coordinate pairs are invalid and could # overflow. try: lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n))) except OverflowError: raise ValueError('Invalid tile coordinate for zoom level %d' % zoom) lat = math.degrees(lat_rad) return lon, lat
def to_lonlat(xtile, ytile, zoom): """Returns a tuple of (longitude, latitude) from a map tile xyz coordinate. See http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_2 Arguments: xtile - x tile location as int or float ytile - y tile location as int or float zoom - zoom level as int or float """ n = 2.0 ** zoom lon = xtile / n * 360.0 - 180.0 # Caculate latitude in radians and convert to degrees constrained from -90 # to 90. Values too big for tile coordinate pairs are invalid and could # overflow. try: lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n))) except OverflowError: raise ValueError('Invalid tile coordinate for zoom level %d' % zoom) lat = math.degrees(lat_rad) return lon, lat
[ "Returns", "a", "tuple", "of", "(", "longitude", "latitude", ")", "from", "a", "map", "tile", "xyz", "coordinate", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/tile.py#L20-L40
[ "def", "to_lonlat", "(", "xtile", ",", "ytile", ",", "zoom", ")", ":", "n", "=", "2.0", "**", "zoom", "lon", "=", "xtile", "/", "n", "*", "360.0", "-", "180.0", "# Caculate latitude in radians and convert to degrees constrained from -90", "# to 90. Values too big for tile coordinate pairs are invalid and could", "# overflow.", "try", ":", "lat_rad", "=", "math", ".", "atan", "(", "math", ".", "sinh", "(", "math", ".", "pi", "*", "(", "1", "-", "2", "*", "ytile", "/", "n", ")", ")", ")", "except", "OverflowError", ":", "raise", "ValueError", "(", "'Invalid tile coordinate for zoom level %d'", "%", "zoom", ")", "lat", "=", "math", ".", "degrees", "(", "lat_rad", ")", "return", "lon", ",", "lat" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
to_tile
Returns a tuple of (xtile, ytile) from a (longitude, latitude) coordinate. See http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames Arguments: lon - longitude as int or float lat - latitude as int or float zoom - zoom level as int or float
greenwich/tile.py
def to_tile(lon, lat, zoom): """Returns a tuple of (xtile, ytile) from a (longitude, latitude) coordinate. See http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames Arguments: lon - longitude as int or float lat - latitude as int or float zoom - zoom level as int or float """ lat_rad = math.radians(lat) n = 2.0 ** zoom xtile = int((lon + 180.0) / 360.0 * n) ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n) return xtile, ytile
def to_tile(lon, lat, zoom): """Returns a tuple of (xtile, ytile) from a (longitude, latitude) coordinate. See http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames Arguments: lon - longitude as int or float lat - latitude as int or float zoom - zoom level as int or float """ lat_rad = math.radians(lat) n = 2.0 ** zoom xtile = int((lon + 180.0) / 360.0 * n) ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n) return xtile, ytile
[ "Returns", "a", "tuple", "of", "(", "xtile", "ytile", ")", "from", "a", "(", "longitude", "latitude", ")", "coordinate", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/tile.py#L42-L57
[ "def", "to_tile", "(", "lon", ",", "lat", ",", "zoom", ")", ":", "lat_rad", "=", "math", ".", "radians", "(", "lat", ")", "n", "=", "2.0", "**", "zoom", "xtile", "=", "int", "(", "(", "lon", "+", "180.0", ")", "/", "360.0", "*", "n", ")", "ytile", "=", "int", "(", "(", "1.0", "-", "math", ".", "log", "(", "math", ".", "tan", "(", "lat_rad", ")", "+", "(", "1", "/", "math", ".", "cos", "(", "lat_rad", ")", ")", ")", "/", "math", ".", "pi", ")", "/", "2.0", "*", "n", ")", "return", "xtile", ",", "ytile" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
extract_hbs
Extract messages from Handlebars templates. It returns an iterator yielding tuples in the following form ``(lineno, funcname, message, comments)``. TODO: Things to improve: --- Return comments
pybabel_hbs/extractor.py
def extract_hbs(fileobj, keywords, comment_tags, options): """Extract messages from Handlebars templates. It returns an iterator yielding tuples in the following form ``(lineno, funcname, message, comments)``. TODO: Things to improve: --- Return comments """ server = get_pipeserver() server.sendline(COMMAND+u'PARSE FILE:'+fileobj.name) server.expect(RESPONSE+'SENDING OUTPUT') server.expect(RESPONSE+'OUTPUT END') trans_strings = server.before for item in json.loads(trans_strings): messages = [item['content']] if item['funcname'] == 'ngettext': messages.append(item['alt_content']) yield item['line_number'],item['funcname'],tuple(messages),[]
def extract_hbs(fileobj, keywords, comment_tags, options): """Extract messages from Handlebars templates. It returns an iterator yielding tuples in the following form ``(lineno, funcname, message, comments)``. TODO: Things to improve: --- Return comments """ server = get_pipeserver() server.sendline(COMMAND+u'PARSE FILE:'+fileobj.name) server.expect(RESPONSE+'SENDING OUTPUT') server.expect(RESPONSE+'OUTPUT END') trans_strings = server.before for item in json.loads(trans_strings): messages = [item['content']] if item['funcname'] == 'ngettext': messages.append(item['alt_content']) yield item['line_number'],item['funcname'],tuple(messages),[]
[ "Extract", "messages", "from", "Handlebars", "templates", "." ]
tigrawap/pybabel-hbs
python
https://github.com/tigrawap/pybabel-hbs/blob/5289bc0f8a5e97044f5b074c9532dcd2115625b9/pybabel_hbs/extractor.py#L34-L54
[ "def", "extract_hbs", "(", "fileobj", ",", "keywords", ",", "comment_tags", ",", "options", ")", ":", "server", "=", "get_pipeserver", "(", ")", "server", ".", "sendline", "(", "COMMAND", "+", "u'PARSE FILE:'", "+", "fileobj", ".", "name", ")", "server", ".", "expect", "(", "RESPONSE", "+", "'SENDING OUTPUT'", ")", "server", ".", "expect", "(", "RESPONSE", "+", "'OUTPUT END'", ")", "trans_strings", "=", "server", ".", "before", "for", "item", "in", "json", ".", "loads", "(", "trans_strings", ")", ":", "messages", "=", "[", "item", "[", "'content'", "]", "]", "if", "item", "[", "'funcname'", "]", "==", "'ngettext'", ":", "messages", ".", "append", "(", "item", "[", "'alt_content'", "]", ")", "yield", "item", "[", "'line_number'", "]", ",", "item", "[", "'funcname'", "]", ",", "tuple", "(", "messages", ")", ",", "[", "]" ]
5289bc0f8a5e97044f5b074c9532dcd2115625b9
test
vsiprefix
Returns a GDAL virtual filesystem prefixed path. Arguments: path -- file path as str
greenwich/io.py
def vsiprefix(path): """Returns a GDAL virtual filesystem prefixed path. Arguments: path -- file path as str """ vpath = path.lower() scheme = VSI_SCHEMES.get(urlparse(vpath).scheme, '') for ext in VSI_TYPES: if ext in vpath: filesys = VSI_TYPES[ext] break else: filesys = '' if filesys and scheme: filesys = filesys[:-1] return ''.join((filesys, scheme, path))
def vsiprefix(path): """Returns a GDAL virtual filesystem prefixed path. Arguments: path -- file path as str """ vpath = path.lower() scheme = VSI_SCHEMES.get(urlparse(vpath).scheme, '') for ext in VSI_TYPES: if ext in vpath: filesys = VSI_TYPES[ext] break else: filesys = '' if filesys and scheme: filesys = filesys[:-1] return ''.join((filesys, scheme, path))
[ "Returns", "a", "GDAL", "virtual", "filesystem", "prefixed", "path", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/io.py#L16-L32
[ "def", "vsiprefix", "(", "path", ")", ":", "vpath", "=", "path", ".", "lower", "(", ")", "scheme", "=", "VSI_SCHEMES", ".", "get", "(", "urlparse", "(", "vpath", ")", ".", "scheme", ",", "''", ")", "for", "ext", "in", "VSI_TYPES", ":", "if", "ext", "in", "vpath", ":", "filesys", "=", "VSI_TYPES", "[", "ext", "]", "break", "else", ":", "filesys", "=", "''", "if", "filesys", "and", "scheme", ":", "filesys", "=", "filesys", "[", ":", "-", "1", "]", "return", "''", ".", "join", "(", "(", "filesys", ",", "scheme", ",", "path", ")", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
SpatialReference.srid
Returns the EPSG ID as int if it exists.
greenwich/srs.py
def srid(self): """Returns the EPSG ID as int if it exists.""" epsg_id = (self.GetAuthorityCode('PROJCS') or self.GetAuthorityCode('GEOGCS')) try: return int(epsg_id) except TypeError: return
def srid(self): """Returns the EPSG ID as int if it exists.""" epsg_id = (self.GetAuthorityCode('PROJCS') or self.GetAuthorityCode('GEOGCS')) try: return int(epsg_id) except TypeError: return
[ "Returns", "the", "EPSG", "ID", "as", "int", "if", "it", "exists", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/srs.py#L44-L51
[ "def", "srid", "(", "self", ")", ":", "epsg_id", "=", "(", "self", ".", "GetAuthorityCode", "(", "'PROJCS'", ")", "or", "self", ".", "GetAuthorityCode", "(", "'GEOGCS'", ")", ")", "try", ":", "return", "int", "(", "epsg_id", ")", "except", "TypeError", ":", "return" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
main
Main entry point for the CLI.
pwm/cli.py
def main(): """ Main entry point for the CLI. """ args = get_args() ret_code = args.target(args) _logger.debug('Exiting with code %d', ret_code) sys.exit(ret_code)
def main(): """ Main entry point for the CLI. """ args = get_args() ret_code = args.target(args) _logger.debug('Exiting with code %d', ret_code) sys.exit(ret_code)
[ "Main", "entry", "point", "for", "the", "CLI", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/cli.py#L30-L35
[ "def", "main", "(", ")", ":", "args", "=", "get_args", "(", ")", "ret_code", "=", "args", ".", "target", "(", "args", ")", "_logger", ".", "debug", "(", "'Exiting with code %d'", ",", "ret_code", ")", "sys", ".", "exit", "(", "ret_code", ")" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
_init_logging
Initialize loggers.
pwm/cli.py
def _init_logging(verbose=False): """ Initialize loggers. """ config = { 'version': 1, 'formatters': { 'console': { 'format': '* %(message)s', } }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'level': 'DEBUG', 'formatter': 'console', 'stream': 'ext://sys.stdout', } }, 'loggers': { 'pwm': { 'level': 'DEBUG' if verbose else 'INFO', 'handlers': ['console'], 'propagate': True, }, 'requests.packages.urllib3': { 'level': 'INFO' if verbose else 'WARNING', 'handlers': ['console'], 'propagate': True, } } } logging.config.dictConfig(config) HTTPConnection.debuglevel = 1 if verbose else 0
def _init_logging(verbose=False): """ Initialize loggers. """ config = { 'version': 1, 'formatters': { 'console': { 'format': '* %(message)s', } }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'level': 'DEBUG', 'formatter': 'console', 'stream': 'ext://sys.stdout', } }, 'loggers': { 'pwm': { 'level': 'DEBUG' if verbose else 'INFO', 'handlers': ['console'], 'propagate': True, }, 'requests.packages.urllib3': { 'level': 'INFO' if verbose else 'WARNING', 'handlers': ['console'], 'propagate': True, } } } logging.config.dictConfig(config) HTTPConnection.debuglevel = 1 if verbose else 0
[ "Initialize", "loggers", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/cli.py#L207-L238
[ "def", "_init_logging", "(", "verbose", "=", "False", ")", ":", "config", "=", "{", "'version'", ":", "1", ",", "'formatters'", ":", "{", "'console'", ":", "{", "'format'", ":", "'* %(message)s'", ",", "}", "}", ",", "'handlers'", ":", "{", "'console'", ":", "{", "'class'", ":", "'logging.StreamHandler'", ",", "'level'", ":", "'DEBUG'", ",", "'formatter'", ":", "'console'", ",", "'stream'", ":", "'ext://sys.stdout'", ",", "}", "}", ",", "'loggers'", ":", "{", "'pwm'", ":", "{", "'level'", ":", "'DEBUG'", "if", "verbose", "else", "'INFO'", ",", "'handlers'", ":", "[", "'console'", "]", ",", "'propagate'", ":", "True", ",", "}", ",", "'requests.packages.urllib3'", ":", "{", "'level'", ":", "'INFO'", "if", "verbose", "else", "'WARNING'", ",", "'handlers'", ":", "[", "'console'", "]", ",", "'propagate'", ":", "True", ",", "}", "}", "}", "logging", ".", "config", ".", "dictConfig", "(", "config", ")", "HTTPConnection", ".", "debuglevel", "=", "1", "if", "verbose", "else", "0" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
update_file
Update the content of a single file.
scripts/update-lists.py
def update_file(url, filename): """Update the content of a single file.""" resp = urlopen(url) if resp.code != 200: raise Exception('GET {} failed.'.format(url)) with open(_get_package_path(filename), 'w') as fp: for l in resp: if not l.startswith(b'#'): fp.write(l.decode('utf8')) print('Updated {}'.format(filename))
def update_file(url, filename): """Update the content of a single file.""" resp = urlopen(url) if resp.code != 200: raise Exception('GET {} failed.'.format(url)) with open(_get_package_path(filename), 'w') as fp: for l in resp: if not l.startswith(b'#'): fp.write(l.decode('utf8')) print('Updated {}'.format(filename))
[ "Update", "the", "content", "of", "a", "single", "file", "." ]
inveniosoftware/counter-robots
python
https://github.com/inveniosoftware/counter-robots/blob/484943fdc7e08f41d3ad7a9e2229afe0cec05547/scripts/update-lists.py#L43-L52
[ "def", "update_file", "(", "url", ",", "filename", ")", ":", "resp", "=", "urlopen", "(", "url", ")", "if", "resp", ".", "code", "!=", "200", ":", "raise", "Exception", "(", "'GET {} failed.'", ".", "format", "(", "url", ")", ")", "with", "open", "(", "_get_package_path", "(", "filename", ")", ",", "'w'", ")", "as", "fp", ":", "for", "l", "in", "resp", ":", "if", "not", "l", ".", "startswith", "(", "b'#'", ")", ":", "fp", ".", "write", "(", "l", ".", "decode", "(", "'utf8'", ")", ")", "print", "(", "'Updated {}'", ".", "format", "(", "filename", ")", ")" ]
484943fdc7e08f41d3ad7a9e2229afe0cec05547
test
available_drivers
Returns a dictionary of enabled GDAL Driver metadata keyed by the 'ShortName' attribute.
greenwich/raster.py
def available_drivers(): """Returns a dictionary of enabled GDAL Driver metadata keyed by the 'ShortName' attribute. """ drivers = {} for i in range(gdal.GetDriverCount()): d = gdal.GetDriver(i) drivers[d.ShortName] = d.GetMetadata() return drivers
def available_drivers(): """Returns a dictionary of enabled GDAL Driver metadata keyed by the 'ShortName' attribute. """ drivers = {} for i in range(gdal.GetDriverCount()): d = gdal.GetDriver(i) drivers[d.ShortName] = d.GetMetadata() return drivers
[ "Returns", "a", "dictionary", "of", "enabled", "GDAL", "Driver", "metadata", "keyed", "by", "the", "ShortName", "attribute", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L18-L26
[ "def", "available_drivers", "(", ")", ":", "drivers", "=", "{", "}", "for", "i", "in", "range", "(", "gdal", ".", "GetDriverCount", "(", ")", ")", ":", "d", "=", "gdal", ".", "GetDriver", "(", "i", ")", "drivers", "[", "d", ".", "ShortName", "]", "=", "d", ".", "GetMetadata", "(", ")", "return", "drivers" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
driver_for_path
Returns the gdal.Driver for a path or None based on the file extension. Arguments: path -- file path as str with a GDAL supported file extension
greenwich/raster.py
def driver_for_path(path, drivers=None): """Returns the gdal.Driver for a path or None based on the file extension. Arguments: path -- file path as str with a GDAL supported file extension """ ext = (os.path.splitext(path)[1][1:] or path).lower() drivers = drivers or ImageDriver.registry if ext else {} for name, meta in drivers.items(): if ext == meta.get('DMD_EXTENSION', '').lower(): return ImageDriver(name) return None
def driver_for_path(path, drivers=None): """Returns the gdal.Driver for a path or None based on the file extension. Arguments: path -- file path as str with a GDAL supported file extension """ ext = (os.path.splitext(path)[1][1:] or path).lower() drivers = drivers or ImageDriver.registry if ext else {} for name, meta in drivers.items(): if ext == meta.get('DMD_EXTENSION', '').lower(): return ImageDriver(name) return None
[ "Returns", "the", "gdal", ".", "Driver", "for", "a", "path", "or", "None", "based", "on", "the", "file", "extension", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L28-L39
[ "def", "driver_for_path", "(", "path", ",", "drivers", "=", "None", ")", ":", "ext", "=", "(", "os", ".", "path", ".", "splitext", "(", "path", ")", "[", "1", "]", "[", "1", ":", "]", "or", "path", ")", ".", "lower", "(", ")", "drivers", "=", "drivers", "or", "ImageDriver", ".", "registry", "if", "ext", "else", "{", "}", "for", "name", ",", "meta", "in", "drivers", ".", "items", "(", ")", ":", "if", "ext", "==", "meta", ".", "get", "(", "'DMD_EXTENSION'", ",", "''", ")", ".", "lower", "(", ")", ":", "return", "ImageDriver", "(", "name", ")", "return", "None" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
geom_to_array
Converts an OGR polygon to a 2D NumPy array. Arguments: geom -- OGR Geometry size -- array size in pixels as a tuple of (width, height) affine -- AffineTransform
greenwich/raster.py
def geom_to_array(geom, size, affine): """Converts an OGR polygon to a 2D NumPy array. Arguments: geom -- OGR Geometry size -- array size in pixels as a tuple of (width, height) affine -- AffineTransform """ driver = ImageDriver('MEM') rast = driver.raster(driver.ShortName, size) rast.affine = affine rast.sref = geom.GetSpatialReference() with MemoryLayer.from_records([(1, geom)]) as ml: status = gdal.RasterizeLayer(rast.ds, (1,), ml.layer, burn_values=(1,)) arr = rast.array() rast.close() return arr
def geom_to_array(geom, size, affine): """Converts an OGR polygon to a 2D NumPy array. Arguments: geom -- OGR Geometry size -- array size in pixels as a tuple of (width, height) affine -- AffineTransform """ driver = ImageDriver('MEM') rast = driver.raster(driver.ShortName, size) rast.affine = affine rast.sref = geom.GetSpatialReference() with MemoryLayer.from_records([(1, geom)]) as ml: status = gdal.RasterizeLayer(rast.ds, (1,), ml.layer, burn_values=(1,)) arr = rast.array() rast.close() return arr
[ "Converts", "an", "OGR", "polygon", "to", "a", "2D", "NumPy", "array", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L45-L61
[ "def", "geom_to_array", "(", "geom", ",", "size", ",", "affine", ")", ":", "driver", "=", "ImageDriver", "(", "'MEM'", ")", "rast", "=", "driver", ".", "raster", "(", "driver", ".", "ShortName", ",", "size", ")", "rast", ".", "affine", "=", "affine", "rast", ".", "sref", "=", "geom", ".", "GetSpatialReference", "(", ")", "with", "MemoryLayer", ".", "from_records", "(", "[", "(", "1", ",", "geom", ")", "]", ")", "as", "ml", ":", "status", "=", "gdal", ".", "RasterizeLayer", "(", "rast", ".", "ds", ",", "(", "1", ",", ")", ",", "ml", ".", "layer", ",", "burn_values", "=", "(", "1", ",", ")", ")", "arr", "=", "rast", ".", "array", "(", ")", "rast", ".", "close", "(", ")", "return", "arr" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
rasterize
Returns a Raster from layer features. Arguments: layer -- Layer to rasterize rast -- Raster with target affine, size, and sref
greenwich/raster.py
def rasterize(layer, rast): """Returns a Raster from layer features. Arguments: layer -- Layer to rasterize rast -- Raster with target affine, size, and sref """ driver = ImageDriver('MEM') r2 = driver.raster(driver.ShortName, rast.size) r2.affine = rast.affine sref = rast.sref if not sref.srid: sref = SpatialReference(4326) r2.sref = sref ml = MemoryLayer(sref, layer.GetGeomType()) ml.load(layer) status = gdal.RasterizeLayer( r2.ds, (1,), ml.layer, options=['ATTRIBUTE=%s' % ml.id]) ml.close() return r2
def rasterize(layer, rast): """Returns a Raster from layer features. Arguments: layer -- Layer to rasterize rast -- Raster with target affine, size, and sref """ driver = ImageDriver('MEM') r2 = driver.raster(driver.ShortName, rast.size) r2.affine = rast.affine sref = rast.sref if not sref.srid: sref = SpatialReference(4326) r2.sref = sref ml = MemoryLayer(sref, layer.GetGeomType()) ml.load(layer) status = gdal.RasterizeLayer( r2.ds, (1,), ml.layer, options=['ATTRIBUTE=%s' % ml.id]) ml.close() return r2
[ "Returns", "a", "Raster", "from", "layer", "features", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L63-L82
[ "def", "rasterize", "(", "layer", ",", "rast", ")", ":", "driver", "=", "ImageDriver", "(", "'MEM'", ")", "r2", "=", "driver", ".", "raster", "(", "driver", ".", "ShortName", ",", "rast", ".", "size", ")", "r2", ".", "affine", "=", "rast", ".", "affine", "sref", "=", "rast", ".", "sref", "if", "not", "sref", ".", "srid", ":", "sref", "=", "SpatialReference", "(", "4326", ")", "r2", ".", "sref", "=", "sref", "ml", "=", "MemoryLayer", "(", "sref", ",", "layer", ".", "GetGeomType", "(", ")", ")", "ml", ".", "load", "(", "layer", ")", "status", "=", "gdal", ".", "RasterizeLayer", "(", "r2", ".", "ds", ",", "(", "1", ",", ")", ",", "ml", ".", "layer", ",", "options", "=", "[", "'ATTRIBUTE=%s'", "%", "ml", ".", "id", "]", ")", "ml", ".", "close", "(", ")", "return", "r2" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
open
Returns a Raster instance. Arguments: path -- local or remote path as str or file-like object Keyword args: mode -- gdal constant representing access mode
greenwich/raster.py
def open(path, mode=gdalconst.GA_ReadOnly): """Returns a Raster instance. Arguments: path -- local or remote path as str or file-like object Keyword args: mode -- gdal constant representing access mode """ path = getattr(path, 'name', path) try: return Raster(vsiprefix(path), mode) except AttributeError: try: imgdata = path.read() except AttributeError: raise TypeError('Not a file-like object providing read()') else: imgio = MemFileIO(delete=False) gdal.FileFromMemBuffer(imgio.name, imgdata) return Raster(imgio, mode) raise ValueError('Failed to open raster from "%r"' % path)
def open(path, mode=gdalconst.GA_ReadOnly): """Returns a Raster instance. Arguments: path -- local or remote path as str or file-like object Keyword args: mode -- gdal constant representing access mode """ path = getattr(path, 'name', path) try: return Raster(vsiprefix(path), mode) except AttributeError: try: imgdata = path.read() except AttributeError: raise TypeError('Not a file-like object providing read()') else: imgio = MemFileIO(delete=False) gdal.FileFromMemBuffer(imgio.name, imgdata) return Raster(imgio, mode) raise ValueError('Failed to open raster from "%r"' % path)
[ "Returns", "a", "Raster", "instance", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L685-L705
[ "def", "open", "(", "path", ",", "mode", "=", "gdalconst", ".", "GA_ReadOnly", ")", ":", "path", "=", "getattr", "(", "path", ",", "'name'", ",", "path", ")", "try", ":", "return", "Raster", "(", "vsiprefix", "(", "path", ")", ",", "mode", ")", "except", "AttributeError", ":", "try", ":", "imgdata", "=", "path", ".", "read", "(", ")", "except", "AttributeError", ":", "raise", "TypeError", "(", "'Not a file-like object providing read()'", ")", "else", ":", "imgio", "=", "MemFileIO", "(", "delete", "=", "False", ")", "gdal", ".", "FileFromMemBuffer", "(", "imgio", ".", "name", ",", "imgdata", ")", "return", "Raster", "(", "imgio", ",", "mode", ")", "raise", "ValueError", "(", "'Failed to open raster from \"%r\"'", "%", "path", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
frombytes
Returns an in-memory raster initialized from a pixel buffer. Arguments: data -- byte buffer of raw pixel data size -- two or three-tuple of (xsize, ysize, bandcount) bandtype -- band data type
greenwich/raster.py
def frombytes(data, size, bandtype=gdal.GDT_Byte): """Returns an in-memory raster initialized from a pixel buffer. Arguments: data -- byte buffer of raw pixel data size -- two or three-tuple of (xsize, ysize, bandcount) bandtype -- band data type """ r = ImageDriver('MEM').raster('', size, bandtype) r.frombytes(data) return r
def frombytes(data, size, bandtype=gdal.GDT_Byte): """Returns an in-memory raster initialized from a pixel buffer. Arguments: data -- byte buffer of raw pixel data size -- two or three-tuple of (xsize, ysize, bandcount) bandtype -- band data type """ r = ImageDriver('MEM').raster('', size, bandtype) r.frombytes(data) return r
[ "Returns", "an", "in", "-", "memory", "raster", "initialized", "from", "a", "pixel", "buffer", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L707-L717
[ "def", "frombytes", "(", "data", ",", "size", ",", "bandtype", "=", "gdal", ".", "GDT_Byte", ")", ":", "r", "=", "ImageDriver", "(", "'MEM'", ")", ".", "raster", "(", "''", ",", "size", ",", "bandtype", ")", "r", ".", "frombytes", "(", "data", ")", "return", "r" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
AffineTransform.project
Convert image pixel/line coordinates to georeferenced x/y, return a generator of two-tuples. Arguments: coords -- input coordinates as iterable containing two-tuples/lists such as ((0, 0), (10, 10))
greenwich/raster.py
def project(self, coords): """Convert image pixel/line coordinates to georeferenced x/y, return a generator of two-tuples. Arguments: coords -- input coordinates as iterable containing two-tuples/lists such as ((0, 0), (10, 10)) """ geotransform = self.tuple for x, y in coords: geo_x = geotransform[0] + geotransform[1] * x + geotransform[2] * y geo_y = geotransform[3] + geotransform[4] * x + geotransform[5] * y # Move the coordinate to the center of the pixel. geo_x += geotransform[1] / 2.0 geo_y += geotransform[5] / 2.0 yield geo_x, geo_y
def project(self, coords): """Convert image pixel/line coordinates to georeferenced x/y, return a generator of two-tuples. Arguments: coords -- input coordinates as iterable containing two-tuples/lists such as ((0, 0), (10, 10)) """ geotransform = self.tuple for x, y in coords: geo_x = geotransform[0] + geotransform[1] * x + geotransform[2] * y geo_y = geotransform[3] + geotransform[4] * x + geotransform[5] * y # Move the coordinate to the center of the pixel. geo_x += geotransform[1] / 2.0 geo_y += geotransform[5] / 2.0 yield geo_x, geo_y
[ "Convert", "image", "pixel", "/", "line", "coordinates", "to", "georeferenced", "x", "/", "y", "return", "a", "generator", "of", "two", "-", "tuples", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L129-L144
[ "def", "project", "(", "self", ",", "coords", ")", ":", "geotransform", "=", "self", ".", "tuple", "for", "x", ",", "y", "in", "coords", ":", "geo_x", "=", "geotransform", "[", "0", "]", "+", "geotransform", "[", "1", "]", "*", "x", "+", "geotransform", "[", "2", "]", "*", "y", "geo_y", "=", "geotransform", "[", "3", "]", "+", "geotransform", "[", "4", "]", "*", "x", "+", "geotransform", "[", "5", "]", "*", "y", "# Move the coordinate to the center of the pixel.", "geo_x", "+=", "geotransform", "[", "1", "]", "/", "2.0", "geo_y", "+=", "geotransform", "[", "5", "]", "/", "2.0", "yield", "geo_x", ",", "geo_y" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
AffineTransform.transform
Transform from projection coordinates (Xp,Yp) space to pixel/line (P,L) raster space, based on the provided geotransformation. Arguments: coords -- input coordinates as iterable containing two-tuples/lists such as ((-120, 38), (-121, 39))
greenwich/raster.py
def transform(self, coords): """Transform from projection coordinates (Xp,Yp) space to pixel/line (P,L) raster space, based on the provided geotransformation. Arguments: coords -- input coordinates as iterable containing two-tuples/lists such as ((-120, 38), (-121, 39)) """ # Use local vars for better performance here. origin_x, origin_y = self.origin sx, sy = self.scale return [(int(math.floor((x - origin_x) / sx)), int(math.floor((y - origin_y) / sy))) for x, y in coords]
def transform(self, coords): """Transform from projection coordinates (Xp,Yp) space to pixel/line (P,L) raster space, based on the provided geotransformation. Arguments: coords -- input coordinates as iterable containing two-tuples/lists such as ((-120, 38), (-121, 39)) """ # Use local vars for better performance here. origin_x, origin_y = self.origin sx, sy = self.scale return [(int(math.floor((x - origin_x) / sx)), int(math.floor((y - origin_y) / sy))) for x, y in coords]
[ "Transform", "from", "projection", "coordinates", "(", "Xp", "Yp", ")", "space", "to", "pixel", "/", "line", "(", "P", "L", ")", "raster", "space", "based", "on", "the", "provided", "geotransformation", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L146-L159
[ "def", "transform", "(", "self", ",", "coords", ")", ":", "# Use local vars for better performance here.", "origin_x", ",", "origin_y", "=", "self", ".", "origin", "sx", ",", "sy", "=", "self", ".", "scale", "return", "[", "(", "int", "(", "math", ".", "floor", "(", "(", "x", "-", "origin_x", ")", "/", "sx", ")", ")", ",", "int", "(", "math", ".", "floor", "(", "(", "y", "-", "origin_y", ")", "/", "sy", ")", ")", ")", "for", "x", ",", "y", "in", "coords", "]" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
ImageDriver.copy
Returns a copied Raster instance. Arguments: source -- the source Raster instance or filepath as str dest -- destination filepath as str
greenwich/raster.py
def copy(self, source, dest): """Returns a copied Raster instance. Arguments: source -- the source Raster instance or filepath as str dest -- destination filepath as str """ if not self.copyable: raise IOError('Driver does not support raster copying') if not isinstance(source, Raster): source = Raster(source) should_close = True else: should_close = False if source.name == dest: raise ValueError( 'Input and output are the same location: %s' % source.name) settings = driverdict_tolist(self.settings) ds = self.CreateCopy(dest, source.ds, self.strictmode, options=settings) if should_close: source.close() return Raster(ds)
def copy(self, source, dest): """Returns a copied Raster instance. Arguments: source -- the source Raster instance or filepath as str dest -- destination filepath as str """ if not self.copyable: raise IOError('Driver does not support raster copying') if not isinstance(source, Raster): source = Raster(source) should_close = True else: should_close = False if source.name == dest: raise ValueError( 'Input and output are the same location: %s' % source.name) settings = driverdict_tolist(self.settings) ds = self.CreateCopy(dest, source.ds, self.strictmode, options=settings) if should_close: source.close() return Raster(ds)
[ "Returns", "a", "copied", "Raster", "instance", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L204-L226
[ "def", "copy", "(", "self", ",", "source", ",", "dest", ")", ":", "if", "not", "self", ".", "copyable", ":", "raise", "IOError", "(", "'Driver does not support raster copying'", ")", "if", "not", "isinstance", "(", "source", ",", "Raster", ")", ":", "source", "=", "Raster", "(", "source", ")", "should_close", "=", "True", "else", ":", "should_close", "=", "False", "if", "source", ".", "name", "==", "dest", ":", "raise", "ValueError", "(", "'Input and output are the same location: %s'", "%", "source", ".", "name", ")", "settings", "=", "driverdict_tolist", "(", "self", ".", "settings", ")", "ds", "=", "self", ".", "CreateCopy", "(", "dest", ",", "source", ".", "ds", ",", "self", ".", "strictmode", ",", "options", "=", "settings", ")", "if", "should_close", ":", "source", ".", "close", "(", ")", "return", "Raster", "(", "ds", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
ImageDriver.Create
Calls Driver.Create() with optionally provided creation options as dict, or falls back to driver specific defaults.
greenwich/raster.py
def Create(self, *args, **kwargs): """Calls Driver.Create() with optionally provided creation options as dict, or falls back to driver specific defaults. """ if not self.writable: raise IOError('Driver does not support raster creation') options = kwargs.pop('options', {}) kwargs['options'] = driverdict_tolist(options or self.settings) return self._driver.Create(*args, **kwargs)
def Create(self, *args, **kwargs): """Calls Driver.Create() with optionally provided creation options as dict, or falls back to driver specific defaults. """ if not self.writable: raise IOError('Driver does not support raster creation') options = kwargs.pop('options', {}) kwargs['options'] = driverdict_tolist(options or self.settings) return self._driver.Create(*args, **kwargs)
[ "Calls", "Driver", ".", "Create", "()", "with", "optionally", "provided", "creation", "options", "as", "dict", "or", "falls", "back", "to", "driver", "specific", "defaults", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L228-L236
[ "def", "Create", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "if", "not", "self", ".", "writable", ":", "raise", "IOError", "(", "'Driver does not support raster creation'", ")", "options", "=", "kwargs", ".", "pop", "(", "'options'", ",", "{", "}", ")", "kwargs", "[", "'options'", "]", "=", "driverdict_tolist", "(", "options", "or", "self", ".", "settings", ")", "return", "self", ".", "_driver", ".", "Create", "(", "*", "args", ",", "*", "*", "kwargs", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
ImageDriver.options
Returns a dict of driver specific raster creation options. See GDAL format docs at http://www.gdal.org/formats_list.html
greenwich/raster.py
def options(self): """Returns a dict of driver specific raster creation options. See GDAL format docs at http://www.gdal.org/formats_list.html """ if self._options is None: try: elem = ET.fromstring( self.info.get('DMD_CREATIONOPTIONLIST', '')) except ET.ParseError: elem = [] opts = {} for child in elem: choices = [val.text for val in child] if choices: child.attrib.update(choices=choices) opts[child.attrib.pop('name')] = child.attrib self._options = opts return self._options
def options(self): """Returns a dict of driver specific raster creation options. See GDAL format docs at http://www.gdal.org/formats_list.html """ if self._options is None: try: elem = ET.fromstring( self.info.get('DMD_CREATIONOPTIONLIST', '')) except ET.ParseError: elem = [] opts = {} for child in elem: choices = [val.text for val in child] if choices: child.attrib.update(choices=choices) opts[child.attrib.pop('name')] = child.attrib self._options = opts return self._options
[ "Returns", "a", "dict", "of", "driver", "specific", "raster", "creation", "options", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L253-L271
[ "def", "options", "(", "self", ")", ":", "if", "self", ".", "_options", "is", "None", ":", "try", ":", "elem", "=", "ET", ".", "fromstring", "(", "self", ".", "info", ".", "get", "(", "'DMD_CREATIONOPTIONLIST'", ",", "''", ")", ")", "except", "ET", ".", "ParseError", ":", "elem", "=", "[", "]", "opts", "=", "{", "}", "for", "child", "in", "elem", ":", "choices", "=", "[", "val", ".", "text", "for", "val", "in", "child", "]", "if", "choices", ":", "child", ".", "attrib", ".", "update", "(", "choices", "=", "choices", ")", "opts", "[", "child", ".", "attrib", ".", "pop", "(", "'name'", ")", "]", "=", "child", ".", "attrib", "self", ".", "_options", "=", "opts", "return", "self", ".", "_options" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
ImageDriver.raster
Returns a new Raster instance. gdal.Driver.Create() does not support all formats. Arguments: path -- file object or path as str size -- two or three-tuple of (xsize, ysize, bandcount) bandtype -- GDAL pixel data type
greenwich/raster.py
def raster(self, path, size, bandtype=gdal.GDT_Byte): """Returns a new Raster instance. gdal.Driver.Create() does not support all formats. Arguments: path -- file object or path as str size -- two or three-tuple of (xsize, ysize, bandcount) bandtype -- GDAL pixel data type """ path = getattr(path, 'name', path) try: is_multiband = len(size) > 2 nx, ny, nbands = size if is_multiband else size + (1,) except (TypeError, ValueError) as exc: exc.args = ('Size must be 2 or 3-item sequence',) raise if nx < 1 or ny < 1: raise ValueError('Invalid raster size %s' % (size,)) # Do not write to a non-empty file. if not self._is_empty(path): raise IOError('%s already exists, open with Raster()' % path) ds = self.Create(path, nx, ny, nbands, bandtype) if not ds: raise ValueError( 'Could not create %s using %s' % (path, str(self))) return Raster(ds)
def raster(self, path, size, bandtype=gdal.GDT_Byte): """Returns a new Raster instance. gdal.Driver.Create() does not support all formats. Arguments: path -- file object or path as str size -- two or three-tuple of (xsize, ysize, bandcount) bandtype -- GDAL pixel data type """ path = getattr(path, 'name', path) try: is_multiband = len(size) > 2 nx, ny, nbands = size if is_multiband else size + (1,) except (TypeError, ValueError) as exc: exc.args = ('Size must be 2 or 3-item sequence',) raise if nx < 1 or ny < 1: raise ValueError('Invalid raster size %s' % (size,)) # Do not write to a non-empty file. if not self._is_empty(path): raise IOError('%s already exists, open with Raster()' % path) ds = self.Create(path, nx, ny, nbands, bandtype) if not ds: raise ValueError( 'Could not create %s using %s' % (path, str(self))) return Raster(ds)
[ "Returns", "a", "new", "Raster", "instance", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L281-L307
[ "def", "raster", "(", "self", ",", "path", ",", "size", ",", "bandtype", "=", "gdal", ".", "GDT_Byte", ")", ":", "path", "=", "getattr", "(", "path", ",", "'name'", ",", "path", ")", "try", ":", "is_multiband", "=", "len", "(", "size", ")", ">", "2", "nx", ",", "ny", ",", "nbands", "=", "size", "if", "is_multiband", "else", "size", "+", "(", "1", ",", ")", "except", "(", "TypeError", ",", "ValueError", ")", "as", "exc", ":", "exc", ".", "args", "=", "(", "'Size must be 2 or 3-item sequence'", ",", ")", "raise", "if", "nx", "<", "1", "or", "ny", "<", "1", ":", "raise", "ValueError", "(", "'Invalid raster size %s'", "%", "(", "size", ",", ")", ")", "# Do not write to a non-empty file.", "if", "not", "self", ".", "_is_empty", "(", "path", ")", ":", "raise", "IOError", "(", "'%s already exists, open with Raster()'", "%", "path", ")", "ds", "=", "self", ".", "Create", "(", "path", ",", "nx", ",", "ny", ",", "nbands", ",", "bandtype", ")", "if", "not", "ds", ":", "raise", "ValueError", "(", "'Could not create %s using %s'", "%", "(", "path", ",", "str", "(", "self", ")", ")", ")", "return", "Raster", "(", "ds", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.SetGeoTransform
Sets the affine transformation. Intercepts the gdal.Dataset call to ensure use as a property setter. Arguments: affine -- AffineTransform or six-tuple of geotransformation values
greenwich/raster.py
def SetGeoTransform(self, affine): """Sets the affine transformation. Intercepts the gdal.Dataset call to ensure use as a property setter. Arguments: affine -- AffineTransform or six-tuple of geotransformation values """ if isinstance(affine, collections.Sequence): affine = AffineTransform(*affine) self._affine = affine self.ds.SetGeoTransform(affine)
def SetGeoTransform(self, affine): """Sets the affine transformation. Intercepts the gdal.Dataset call to ensure use as a property setter. Arguments: affine -- AffineTransform or six-tuple of geotransformation values """ if isinstance(affine, collections.Sequence): affine = AffineTransform(*affine) self._affine = affine self.ds.SetGeoTransform(affine)
[ "Sets", "the", "affine", "transformation", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L410-L421
[ "def", "SetGeoTransform", "(", "self", ",", "affine", ")", ":", "if", "isinstance", "(", "affine", ",", "collections", ".", "Sequence", ")", ":", "affine", "=", "AffineTransform", "(", "*", "affine", ")", "self", ".", "_affine", "=", "affine", "self", ".", "ds", ".", "SetGeoTransform", "(", "affine", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.array
Returns an NDArray, optionally subset by spatial envelope. Keyword args: envelope -- coordinate extent tuple or Envelope
greenwich/raster.py
def array(self, envelope=()): """Returns an NDArray, optionally subset by spatial envelope. Keyword args: envelope -- coordinate extent tuple or Envelope """ args = () if envelope: args = self.get_offset(envelope) return self.ds.ReadAsArray(*args)
def array(self, envelope=()): """Returns an NDArray, optionally subset by spatial envelope. Keyword args: envelope -- coordinate extent tuple or Envelope """ args = () if envelope: args = self.get_offset(envelope) return self.ds.ReadAsArray(*args)
[ "Returns", "an", "NDArray", "optionally", "subset", "by", "spatial", "envelope", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L425-L434
[ "def", "array", "(", "self", ",", "envelope", "=", "(", ")", ")", ":", "args", "=", "(", ")", "if", "envelope", ":", "args", "=", "self", ".", "get_offset", "(", "envelope", ")", "return", "self", ".", "ds", ".", "ReadAsArray", "(", "*", "args", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.envelope
Returns the minimum bounding rectangle as a tuple of min X, min Y, max X, max Y.
greenwich/raster.py
def envelope(self): """Returns the minimum bounding rectangle as a tuple of min X, min Y, max X, max Y. """ if self._envelope is None: origin = self.affine.origin ur_x = origin[0] + self.ds.RasterXSize * self.affine.scale[0] ll_y = origin[1] + self.ds.RasterYSize * self.affine.scale[1] self._envelope = Envelope(origin[0], ll_y, ur_x, origin[1]) return self._envelope
def envelope(self): """Returns the minimum bounding rectangle as a tuple of min X, min Y, max X, max Y. """ if self._envelope is None: origin = self.affine.origin ur_x = origin[0] + self.ds.RasterXSize * self.affine.scale[0] ll_y = origin[1] + self.ds.RasterYSize * self.affine.scale[1] self._envelope = Envelope(origin[0], ll_y, ur_x, origin[1]) return self._envelope
[ "Returns", "the", "minimum", "bounding", "rectangle", "as", "a", "tuple", "of", "min", "X", "min", "Y", "max", "X", "max", "Y", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L459-L468
[ "def", "envelope", "(", "self", ")", ":", "if", "self", ".", "_envelope", "is", "None", ":", "origin", "=", "self", ".", "affine", ".", "origin", "ur_x", "=", "origin", "[", "0", "]", "+", "self", ".", "ds", ".", "RasterXSize", "*", "self", ".", "affine", ".", "scale", "[", "0", "]", "ll_y", "=", "origin", "[", "1", "]", "+", "self", ".", "ds", ".", "RasterYSize", "*", "self", ".", "affine", ".", "scale", "[", "1", "]", "self", ".", "_envelope", "=", "Envelope", "(", "origin", "[", "0", "]", ",", "ll_y", ",", "ur_x", ",", "origin", "[", "1", "]", ")", "return", "self", ".", "_envelope" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.get_offset
Returns a 4-tuple pixel window (x_offset, y_offset, x_size, y_size). Arguments: envelope -- coordinate extent tuple or Envelope
greenwich/raster.py
def get_offset(self, envelope): """Returns a 4-tuple pixel window (x_offset, y_offset, x_size, y_size). Arguments: envelope -- coordinate extent tuple or Envelope """ if isinstance(envelope, collections.Sequence): envelope = Envelope(envelope) if not (self.envelope.contains(envelope) or self.envelope.intersects(envelope)): raise ValueError('Envelope does not intersect with this extent') coords = self.affine.transform((envelope.ul, envelope.lr)) nxy = [(min(dest, size) - origin) or 1 for size, origin, dest in zip(self.size, *coords)] return coords[0] + tuple(nxy)
def get_offset(self, envelope): """Returns a 4-tuple pixel window (x_offset, y_offset, x_size, y_size). Arguments: envelope -- coordinate extent tuple or Envelope """ if isinstance(envelope, collections.Sequence): envelope = Envelope(envelope) if not (self.envelope.contains(envelope) or self.envelope.intersects(envelope)): raise ValueError('Envelope does not intersect with this extent') coords = self.affine.transform((envelope.ul, envelope.lr)) nxy = [(min(dest, size) - origin) or 1 for size, origin, dest in zip(self.size, *coords)] return coords[0] + tuple(nxy)
[ "Returns", "a", "4", "-", "tuple", "pixel", "window", "(", "x_offset", "y_offset", "x_size", "y_size", ")", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L474-L488
[ "def", "get_offset", "(", "self", ",", "envelope", ")", ":", "if", "isinstance", "(", "envelope", ",", "collections", ".", "Sequence", ")", ":", "envelope", "=", "Envelope", "(", "envelope", ")", "if", "not", "(", "self", ".", "envelope", ".", "contains", "(", "envelope", ")", "or", "self", ".", "envelope", ".", "intersects", "(", "envelope", ")", ")", ":", "raise", "ValueError", "(", "'Envelope does not intersect with this extent'", ")", "coords", "=", "self", ".", "affine", ".", "transform", "(", "(", "envelope", ".", "ul", ",", "envelope", ".", "lr", ")", ")", "nxy", "=", "[", "(", "min", "(", "dest", ",", "size", ")", "-", "origin", ")", "or", "1", "for", "size", ",", "origin", ",", "dest", "in", "zip", "(", "self", ".", "size", ",", "*", "coords", ")", "]", "return", "coords", "[", "0", "]", "+", "tuple", "(", "nxy", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.driver
Returns the underlying ImageDriver instance.
greenwich/raster.py
def driver(self): """Returns the underlying ImageDriver instance.""" if self._driver is None: self._driver = ImageDriver(self.ds.GetDriver()) return self._driver
def driver(self): """Returns the underlying ImageDriver instance.""" if self._driver is None: self._driver = ImageDriver(self.ds.GetDriver()) return self._driver
[ "Returns", "the", "underlying", "ImageDriver", "instance", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L491-L495
[ "def", "driver", "(", "self", ")", ":", "if", "self", ".", "_driver", "is", "None", ":", "self", ".", "_driver", "=", "ImageDriver", "(", "self", ".", "ds", ".", "GetDriver", "(", ")", ")", "return", "self", ".", "_driver" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.new
Derive new Raster instances. Keyword args: size -- tuple of image size (width, height) affine -- AffineTransform or six-tuple of geotransformation values
greenwich/raster.py
def new(self, size=(), affine=None): """Derive new Raster instances. Keyword args: size -- tuple of image size (width, height) affine -- AffineTransform or six-tuple of geotransformation values """ size = size or self.size + (len(self),) band = self.ds.GetRasterBand(1) driver = ImageDriver('MEM') rcopy = driver.raster(driver.ShortName, size, band.DataType) rcopy.sref = self.GetProjection() rcopy.affine = affine or tuple(self.affine) colors = band.GetColorTable() for outband in rcopy: if self.nodata is not None: outband.SetNoDataValue(self.nodata) if colors: outband.SetColorTable(colors) return rcopy
def new(self, size=(), affine=None): """Derive new Raster instances. Keyword args: size -- tuple of image size (width, height) affine -- AffineTransform or six-tuple of geotransformation values """ size = size or self.size + (len(self),) band = self.ds.GetRasterBand(1) driver = ImageDriver('MEM') rcopy = driver.raster(driver.ShortName, size, band.DataType) rcopy.sref = self.GetProjection() rcopy.affine = affine or tuple(self.affine) colors = band.GetColorTable() for outband in rcopy: if self.nodata is not None: outband.SetNoDataValue(self.nodata) if colors: outband.SetColorTable(colors) return rcopy
[ "Derive", "new", "Raster", "instances", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L497-L516
[ "def", "new", "(", "self", ",", "size", "=", "(", ")", ",", "affine", "=", "None", ")", ":", "size", "=", "size", "or", "self", ".", "size", "+", "(", "len", "(", "self", ")", ",", ")", "band", "=", "self", ".", "ds", ".", "GetRasterBand", "(", "1", ")", "driver", "=", "ImageDriver", "(", "'MEM'", ")", "rcopy", "=", "driver", ".", "raster", "(", "driver", ".", "ShortName", ",", "size", ",", "band", ".", "DataType", ")", "rcopy", ".", "sref", "=", "self", ".", "GetProjection", "(", ")", "rcopy", ".", "affine", "=", "affine", "or", "tuple", "(", "self", ".", "affine", ")", "colors", "=", "band", ".", "GetColorTable", "(", ")", "for", "outband", "in", "rcopy", ":", "if", "self", ".", "nodata", "is", "not", "None", ":", "outband", ".", "SetNoDataValue", "(", "self", ".", "nodata", ")", "if", "colors", ":", "outband", ".", "SetColorTable", "(", "colors", ")", "return", "rcopy" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.masked_array
Returns a MaskedArray using nodata values. Keyword args: geometry -- any geometry, envelope, or coordinate extent tuple
greenwich/raster.py
def masked_array(self, geometry=None): """Returns a MaskedArray using nodata values. Keyword args: geometry -- any geometry, envelope, or coordinate extent tuple """ if geometry is None: return self._masked_array() geom = transform(geometry, self.sref) env = Envelope.from_geom(geom).intersect(self.envelope) arr = self._masked_array(env) if geom.GetGeometryType() != ogr.wkbPoint: dims = self.get_offset(env)[2:] affine = AffineTransform(*tuple(self.affine)) affine.origin = env.ul mask = ~np.ma.make_mask(geom_to_array(geom, dims, affine)) arr.mask = arr.mask | mask return arr
def masked_array(self, geometry=None): """Returns a MaskedArray using nodata values. Keyword args: geometry -- any geometry, envelope, or coordinate extent tuple """ if geometry is None: return self._masked_array() geom = transform(geometry, self.sref) env = Envelope.from_geom(geom).intersect(self.envelope) arr = self._masked_array(env) if geom.GetGeometryType() != ogr.wkbPoint: dims = self.get_offset(env)[2:] affine = AffineTransform(*tuple(self.affine)) affine.origin = env.ul mask = ~np.ma.make_mask(geom_to_array(geom, dims, affine)) arr.mask = arr.mask | mask return arr
[ "Returns", "a", "MaskedArray", "using", "nodata", "values", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L544-L561
[ "def", "masked_array", "(", "self", ",", "geometry", "=", "None", ")", ":", "if", "geometry", "is", "None", ":", "return", "self", ".", "_masked_array", "(", ")", "geom", "=", "transform", "(", "geometry", ",", "self", ".", "sref", ")", "env", "=", "Envelope", ".", "from_geom", "(", "geom", ")", ".", "intersect", "(", "self", ".", "envelope", ")", "arr", "=", "self", ".", "_masked_array", "(", "env", ")", "if", "geom", ".", "GetGeometryType", "(", ")", "!=", "ogr", ".", "wkbPoint", ":", "dims", "=", "self", ".", "get_offset", "(", "env", ")", "[", "2", ":", "]", "affine", "=", "AffineTransform", "(", "*", "tuple", "(", "self", ".", "affine", ")", ")", "affine", ".", "origin", "=", "env", ".", "ul", "mask", "=", "~", "np", ".", "ma", ".", "make_mask", "(", "geom_to_array", "(", "geom", ",", "dims", ",", "affine", ")", ")", "arr", ".", "mask", "=", "arr", ".", "mask", "|", "mask", "return", "arr" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.nodata
Returns read only property for band nodata value, assuming single band rasters for now.
greenwich/raster.py
def nodata(self): """Returns read only property for band nodata value, assuming single band rasters for now. """ if self._nodata is None: self._nodata = self[0].GetNoDataValue() return self._nodata
def nodata(self): """Returns read only property for band nodata value, assuming single band rasters for now. """ if self._nodata is None: self._nodata = self[0].GetNoDataValue() return self._nodata
[ "Returns", "read", "only", "property", "for", "band", "nodata", "value", "assuming", "single", "band", "rasters", "for", "now", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L564-L570
[ "def", "nodata", "(", "self", ")", ":", "if", "self", ".", "_nodata", "is", "None", ":", "self", ".", "_nodata", "=", "self", "[", "0", "]", ".", "GetNoDataValue", "(", ")", "return", "self", ".", "_nodata" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.ReadRaster
Returns raster data bytes for partial or full extent. Overrides gdal.Dataset.ReadRaster() with the full raster size by default.
greenwich/raster.py
def ReadRaster(self, *args, **kwargs): """Returns raster data bytes for partial or full extent. Overrides gdal.Dataset.ReadRaster() with the full raster size by default. """ args = args or (0, 0, self.ds.RasterXSize, self.ds.RasterYSize) return self.ds.ReadRaster(*args, **kwargs)
def ReadRaster(self, *args, **kwargs): """Returns raster data bytes for partial or full extent. Overrides gdal.Dataset.ReadRaster() with the full raster size by default. """ args = args or (0, 0, self.ds.RasterXSize, self.ds.RasterYSize) return self.ds.ReadRaster(*args, **kwargs)
[ "Returns", "raster", "data", "bytes", "for", "partial", "or", "full", "extent", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L572-L579
[ "def", "ReadRaster", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "args", "=", "args", "or", "(", "0", ",", "0", ",", "self", ".", "ds", ".", "RasterXSize", ",", "self", ".", "ds", ".", "RasterYSize", ")", "return", "self", ".", "ds", ".", "ReadRaster", "(", "*", "args", ",", "*", "*", "kwargs", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.resample
Returns a new instance resampled to provided size. Arguments: size -- tuple of x,y image dimensions
greenwich/raster.py
def resample(self, size, interpolation=gdalconst.GRA_NearestNeighbour): """Returns a new instance resampled to provided size. Arguments: size -- tuple of x,y image dimensions """ # Find the scaling factor for pixel size. factors = (size[0] / float(self.RasterXSize), size[1] / float(self.RasterYSize)) affine = AffineTransform(*tuple(self.affine)) affine.scale = (affine.scale[0] / factors[0], affine.scale[1] / factors[1]) dest = self.new(size, affine) # Uses self and dest projection when set to None gdal.ReprojectImage(self.ds, dest.ds, None, None, interpolation) return dest
def resample(self, size, interpolation=gdalconst.GRA_NearestNeighbour): """Returns a new instance resampled to provided size. Arguments: size -- tuple of x,y image dimensions """ # Find the scaling factor for pixel size. factors = (size[0] / float(self.RasterXSize), size[1] / float(self.RasterYSize)) affine = AffineTransform(*tuple(self.affine)) affine.scale = (affine.scale[0] / factors[0], affine.scale[1] / factors[1]) dest = self.new(size, affine) # Uses self and dest projection when set to None gdal.ReprojectImage(self.ds, dest.ds, None, None, interpolation) return dest
[ "Returns", "a", "new", "instance", "resampled", "to", "provided", "size", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L581-L596
[ "def", "resample", "(", "self", ",", "size", ",", "interpolation", "=", "gdalconst", ".", "GRA_NearestNeighbour", ")", ":", "# Find the scaling factor for pixel size.", "factors", "=", "(", "size", "[", "0", "]", "/", "float", "(", "self", ".", "RasterXSize", ")", ",", "size", "[", "1", "]", "/", "float", "(", "self", ".", "RasterYSize", ")", ")", "affine", "=", "AffineTransform", "(", "*", "tuple", "(", "self", ".", "affine", ")", ")", "affine", ".", "scale", "=", "(", "affine", ".", "scale", "[", "0", "]", "/", "factors", "[", "0", "]", ",", "affine", ".", "scale", "[", "1", "]", "/", "factors", "[", "1", "]", ")", "dest", "=", "self", ".", "new", "(", "size", ",", "affine", ")", "# Uses self and dest projection when set to None", "gdal", ".", "ReprojectImage", "(", "self", ".", "ds", ",", "dest", ".", "ds", ",", "None", ",", "None", ",", "interpolation", ")", "return", "dest" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.save
Save this instance to the path and format provided. Arguments: to -- output path as str, file, or MemFileIO instance Keyword args: driver -- GDAL driver name as string or ImageDriver
greenwich/raster.py
def save(self, to, driver=None): """Save this instance to the path and format provided. Arguments: to -- output path as str, file, or MemFileIO instance Keyword args: driver -- GDAL driver name as string or ImageDriver """ path = getattr(to, 'name', to) if not driver and hasattr(path, 'encode'): driver = driver_for_path(path, self.driver.filter_copyable()) elif hasattr(driver, 'encode'): driver = ImageDriver(driver) if driver is None or not driver.copyable: raise ValueError('Copy supporting driver not found for %s' % path) driver.copy(self, path).close()
def save(self, to, driver=None): """Save this instance to the path and format provided. Arguments: to -- output path as str, file, or MemFileIO instance Keyword args: driver -- GDAL driver name as string or ImageDriver """ path = getattr(to, 'name', to) if not driver and hasattr(path, 'encode'): driver = driver_for_path(path, self.driver.filter_copyable()) elif hasattr(driver, 'encode'): driver = ImageDriver(driver) if driver is None or not driver.copyable: raise ValueError('Copy supporting driver not found for %s' % path) driver.copy(self, path).close()
[ "Save", "this", "instance", "to", "the", "path", "and", "format", "provided", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L598-L613
[ "def", "save", "(", "self", ",", "to", ",", "driver", "=", "None", ")", ":", "path", "=", "getattr", "(", "to", ",", "'name'", ",", "to", ")", "if", "not", "driver", "and", "hasattr", "(", "path", ",", "'encode'", ")", ":", "driver", "=", "driver_for_path", "(", "path", ",", "self", ".", "driver", ".", "filter_copyable", "(", ")", ")", "elif", "hasattr", "(", "driver", ",", "'encode'", ")", ":", "driver", "=", "ImageDriver", "(", "driver", ")", "if", "driver", "is", "None", "or", "not", "driver", ".", "copyable", ":", "raise", "ValueError", "(", "'Copy supporting driver not found for %s'", "%", "path", ")", "driver", ".", "copy", "(", "self", ",", "path", ")", ".", "close", "(", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.SetProjection
Sets the spatial reference. Intercepts the gdal.Dataset call to ensure use as a property setter. Arguments: sref -- SpatialReference or any format supported by the constructor
greenwich/raster.py
def SetProjection(self, sref): """Sets the spatial reference. Intercepts the gdal.Dataset call to ensure use as a property setter. Arguments: sref -- SpatialReference or any format supported by the constructor """ if not hasattr(sref, 'ExportToWkt'): sref = SpatialReference(sref) self._sref = sref self.ds.SetProjection(sref.ExportToWkt())
def SetProjection(self, sref): """Sets the spatial reference. Intercepts the gdal.Dataset call to ensure use as a property setter. Arguments: sref -- SpatialReference or any format supported by the constructor """ if not hasattr(sref, 'ExportToWkt'): sref = SpatialReference(sref) self._sref = sref self.ds.SetProjection(sref.ExportToWkt())
[ "Sets", "the", "spatial", "reference", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L618-L629
[ "def", "SetProjection", "(", "self", ",", "sref", ")", ":", "if", "not", "hasattr", "(", "sref", ",", "'ExportToWkt'", ")", ":", "sref", "=", "SpatialReference", "(", "sref", ")", "self", ".", "_sref", "=", "sref", "self", ".", "ds", ".", "SetProjection", "(", "sref", ".", "ExportToWkt", "(", ")", ")" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.shape
Returns a tuple of row, column, (band count if multidimensional).
greenwich/raster.py
def shape(self): """Returns a tuple of row, column, (band count if multidimensional).""" shp = (self.ds.RasterYSize, self.ds.RasterXSize, self.ds.RasterCount) return shp[:2] if shp[2] <= 1 else shp
def shape(self): """Returns a tuple of row, column, (band count if multidimensional).""" shp = (self.ds.RasterYSize, self.ds.RasterXSize, self.ds.RasterCount) return shp[:2] if shp[2] <= 1 else shp
[ "Returns", "a", "tuple", "of", "row", "column", "(", "band", "count", "if", "multidimensional", ")", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L634-L637
[ "def", "shape", "(", "self", ")", ":", "shp", "=", "(", "self", ".", "ds", ".", "RasterYSize", ",", "self", ".", "ds", ".", "RasterXSize", ",", "self", ".", "ds", ".", "RasterCount", ")", "return", "shp", "[", ":", "2", "]", "if", "shp", "[", "2", "]", "<=", "1", "else", "shp" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
Raster.warp
Returns a new reprojected instance. Arguments: to_sref -- spatial reference as a proj4 or wkt string, or a SpatialReference Keyword args: dest -- filepath as str interpolation -- GDAL interpolation type
greenwich/raster.py
def warp(self, to_sref, dest=None, interpolation=gdalconst.GRA_NearestNeighbour): """Returns a new reprojected instance. Arguments: to_sref -- spatial reference as a proj4 or wkt string, or a SpatialReference Keyword args: dest -- filepath as str interpolation -- GDAL interpolation type """ if not hasattr(to_sref, 'ExportToWkt'): to_sref = SpatialReference(to_sref) dest_wkt = to_sref.ExportToWkt() dtype = self[0].DataType err_thresh = 0.125 # Determine new values for destination raster dimensions and # geotransform. vrt = gdal.AutoCreateWarpedVRT(self.ds, None, dest_wkt, interpolation, err_thresh) if vrt is None: raise ValueError('Could not warp %s to %s' % (self, dest_wkt)) warpsize = (vrt.RasterXSize, vrt.RasterYSize, len(self)) warptrans = vrt.GetGeoTransform() vrt = None if dest is None: imgio = MemFileIO() rwarp = self.driver.raster(imgio, warpsize, dtype) imgio.close() else: rwarp = self.driver.raster(dest, warpsize, dtype) rwarp.SetGeoTransform(warptrans) rwarp.SetProjection(to_sref) if self.nodata is not None: for band in rwarp: band.SetNoDataValue(self.nodata) band = None # Uses self and rwarp projection when set to None gdal.ReprojectImage(self.ds, rwarp.ds, None, None, interpolation) return rwarp
def warp(self, to_sref, dest=None, interpolation=gdalconst.GRA_NearestNeighbour): """Returns a new reprojected instance. Arguments: to_sref -- spatial reference as a proj4 or wkt string, or a SpatialReference Keyword args: dest -- filepath as str interpolation -- GDAL interpolation type """ if not hasattr(to_sref, 'ExportToWkt'): to_sref = SpatialReference(to_sref) dest_wkt = to_sref.ExportToWkt() dtype = self[0].DataType err_thresh = 0.125 # Determine new values for destination raster dimensions and # geotransform. vrt = gdal.AutoCreateWarpedVRT(self.ds, None, dest_wkt, interpolation, err_thresh) if vrt is None: raise ValueError('Could not warp %s to %s' % (self, dest_wkt)) warpsize = (vrt.RasterXSize, vrt.RasterYSize, len(self)) warptrans = vrt.GetGeoTransform() vrt = None if dest is None: imgio = MemFileIO() rwarp = self.driver.raster(imgio, warpsize, dtype) imgio.close() else: rwarp = self.driver.raster(dest, warpsize, dtype) rwarp.SetGeoTransform(warptrans) rwarp.SetProjection(to_sref) if self.nodata is not None: for band in rwarp: band.SetNoDataValue(self.nodata) band = None # Uses self and rwarp projection when set to None gdal.ReprojectImage(self.ds, rwarp.ds, None, None, interpolation) return rwarp
[ "Returns", "a", "new", "reprojected", "instance", "." ]
bkg/greenwich
python
https://github.com/bkg/greenwich/blob/57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141/greenwich/raster.py#L644-L682
[ "def", "warp", "(", "self", ",", "to_sref", ",", "dest", "=", "None", ",", "interpolation", "=", "gdalconst", ".", "GRA_NearestNeighbour", ")", ":", "if", "not", "hasattr", "(", "to_sref", ",", "'ExportToWkt'", ")", ":", "to_sref", "=", "SpatialReference", "(", "to_sref", ")", "dest_wkt", "=", "to_sref", ".", "ExportToWkt", "(", ")", "dtype", "=", "self", "[", "0", "]", ".", "DataType", "err_thresh", "=", "0.125", "# Determine new values for destination raster dimensions and", "# geotransform.", "vrt", "=", "gdal", ".", "AutoCreateWarpedVRT", "(", "self", ".", "ds", ",", "None", ",", "dest_wkt", ",", "interpolation", ",", "err_thresh", ")", "if", "vrt", "is", "None", ":", "raise", "ValueError", "(", "'Could not warp %s to %s'", "%", "(", "self", ",", "dest_wkt", ")", ")", "warpsize", "=", "(", "vrt", ".", "RasterXSize", ",", "vrt", ".", "RasterYSize", ",", "len", "(", "self", ")", ")", "warptrans", "=", "vrt", ".", "GetGeoTransform", "(", ")", "vrt", "=", "None", "if", "dest", "is", "None", ":", "imgio", "=", "MemFileIO", "(", ")", "rwarp", "=", "self", ".", "driver", ".", "raster", "(", "imgio", ",", "warpsize", ",", "dtype", ")", "imgio", ".", "close", "(", ")", "else", ":", "rwarp", "=", "self", ".", "driver", ".", "raster", "(", "dest", ",", "warpsize", ",", "dtype", ")", "rwarp", ".", "SetGeoTransform", "(", "warptrans", ")", "rwarp", ".", "SetProjection", "(", "to_sref", ")", "if", "self", ".", "nodata", "is", "not", "None", ":", "for", "band", "in", "rwarp", ":", "band", ".", "SetNoDataValue", "(", "self", ".", "nodata", ")", "band", "=", "None", "# Uses self and rwarp projection when set to None", "gdal", ".", "ReprojectImage", "(", "self", ".", "ds", ",", "rwarp", ".", "ds", ",", "None", ",", "None", ",", "interpolation", ")", "return", "rwarp" ]
57ec644dadfe43ce0ecf2cfd32a2de71e0c8c141
test
calc_chunklen
computes the ideal conversion ratio for the given alphabet. A ratio is considered ideal when the number of bits in one output encoding chunk that don't add up to one input encoding chunk is minimal.
pwm/encoding.py
def calc_chunklen(alph_len): ''' computes the ideal conversion ratio for the given alphabet. A ratio is considered ideal when the number of bits in one output encoding chunk that don't add up to one input encoding chunk is minimal. ''' binlen, enclen = min([ (i, i*8 / math.log(alph_len, 2)) for i in range(1, 7) ], key=lambda k: k[1] % 1) return binlen, int(enclen)
def calc_chunklen(alph_len): ''' computes the ideal conversion ratio for the given alphabet. A ratio is considered ideal when the number of bits in one output encoding chunk that don't add up to one input encoding chunk is minimal. ''' binlen, enclen = min([ (i, i*8 / math.log(alph_len, 2)) for i in range(1, 7) ], key=lambda k: k[1] % 1) return binlen, int(enclen)
[ "computes", "the", "ideal", "conversion", "ratio", "for", "the", "given", "alphabet", ".", "A", "ratio", "is", "considered", "ideal", "when", "the", "number", "of", "bits", "in", "one", "output", "encoding", "chunk", "that", "don", "t", "add", "up", "to", "one", "input", "encoding", "chunk", "is", "minimal", "." ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/encoding.py#L22-L33
[ "def", "calc_chunklen", "(", "alph_len", ")", ":", "binlen", ",", "enclen", "=", "min", "(", "[", "(", "i", ",", "i", "*", "8", "/", "math", ".", "log", "(", "alph_len", ",", "2", ")", ")", "for", "i", "in", "range", "(", "1", ",", "7", ")", "]", ",", "key", "=", "lambda", "k", ":", "k", "[", "1", "]", "%", "1", ")", "return", "binlen", ",", "int", "(", "enclen", ")" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
lookup_alphabet
retrieves a named charset or treats the input as a custom alphabet and use that
pwm/encoding.py
def lookup_alphabet(charset): ''' retrieves a named charset or treats the input as a custom alphabet and use that ''' if charset in PRESETS: return PRESETS[charset] if len(charset) < 16: _logger.warning('very small alphabet in use, possibly a failed lookup?') return charset
def lookup_alphabet(charset): ''' retrieves a named charset or treats the input as a custom alphabet and use that ''' if charset in PRESETS: return PRESETS[charset] if len(charset) < 16: _logger.warning('very small alphabet in use, possibly a failed lookup?') return charset
[ "retrieves", "a", "named", "charset", "or", "treats", "the", "input", "as", "a", "custom", "alphabet", "and", "use", "that" ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/encoding.py#L89-L97
[ "def", "lookup_alphabet", "(", "charset", ")", ":", "if", "charset", "in", "PRESETS", ":", "return", "PRESETS", "[", "charset", "]", "if", "len", "(", "charset", ")", "<", "16", ":", "_logger", ".", "warning", "(", "'very small alphabet in use, possibly a failed lookup?'", ")", "return", "charset" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
Encoder._encode_chunk
gets a chunk from the input data, converts it to a number and encodes that number
pwm/encoding.py
def _encode_chunk(self, data, index): ''' gets a chunk from the input data, converts it to a number and encodes that number ''' chunk = self._get_chunk(data, index) return self._encode_long(self._chunk_to_long(chunk))
def _encode_chunk(self, data, index): ''' gets a chunk from the input data, converts it to a number and encodes that number ''' chunk = self._get_chunk(data, index) return self._encode_long(self._chunk_to_long(chunk))
[ "gets", "a", "chunk", "from", "the", "input", "data", "converts", "it", "to", "a", "number", "and", "encodes", "that", "number" ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/encoding.py#L55-L61
[ "def", "_encode_chunk", "(", "self", ",", "data", ",", "index", ")", ":", "chunk", "=", "self", ".", "_get_chunk", "(", "data", ",", "index", ")", "return", "self", ".", "_encode_long", "(", "self", ".", "_chunk_to_long", "(", "chunk", ")", ")" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
Encoder._encode_long
encodes an integer of 8*self.chunklen[0] bits using the specified alphabet
pwm/encoding.py
def _encode_long(self, val): ''' encodes an integer of 8*self.chunklen[0] bits using the specified alphabet ''' return ''.join([ self.alphabet[(val//len(self.alphabet)**i) % len(self.alphabet)] for i in reversed(range(self.chunklen[1])) ])
def _encode_long(self, val): ''' encodes an integer of 8*self.chunklen[0] bits using the specified alphabet ''' return ''.join([ self.alphabet[(val//len(self.alphabet)**i) % len(self.alphabet)] for i in reversed(range(self.chunklen[1])) ])
[ "encodes", "an", "integer", "of", "8", "*", "self", ".", "chunklen", "[", "0", "]", "bits", "using", "the", "specified", "alphabet" ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/encoding.py#L63-L71
[ "def", "_encode_long", "(", "self", ",", "val", ")", ":", "return", "''", ".", "join", "(", "[", "self", ".", "alphabet", "[", "(", "val", "//", "len", "(", "self", ".", "alphabet", ")", "**", "i", ")", "%", "len", "(", "self", ".", "alphabet", ")", "]", "for", "i", "in", "reversed", "(", "range", "(", "self", ".", "chunklen", "[", "1", "]", ")", ")", "]", ")" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
Encoder._chunk_to_long
parses a chunk of bytes to integer using big-endian representation
pwm/encoding.py
def _chunk_to_long(self, chunk): ''' parses a chunk of bytes to integer using big-endian representation ''' return sum([ 256**(self.chunklen[0]-1-i) * ord_byte(chunk[i]) for i in range(self.chunklen[0]) ])
def _chunk_to_long(self, chunk): ''' parses a chunk of bytes to integer using big-endian representation ''' return sum([ 256**(self.chunklen[0]-1-i) * ord_byte(chunk[i]) for i in range(self.chunklen[0]) ])
[ "parses", "a", "chunk", "of", "bytes", "to", "integer", "using", "big", "-", "endian", "representation" ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/encoding.py#L73-L80
[ "def", "_chunk_to_long", "(", "self", ",", "chunk", ")", ":", "return", "sum", "(", "[", "256", "**", "(", "self", ".", "chunklen", "[", "0", "]", "-", "1", "-", "i", ")", "*", "ord_byte", "(", "chunk", "[", "i", "]", ")", "for", "i", "in", "range", "(", "self", ".", "chunklen", "[", "0", "]", ")", "]", ")" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
Encoder._get_chunk
partition the data into chunks and retrieve the chunk at the given index
pwm/encoding.py
def _get_chunk(self, data, index): ''' partition the data into chunks and retrieve the chunk at the given index ''' return data[index*self.chunklen[0]:(index+1)*self.chunklen[0]]
def _get_chunk(self, data, index): ''' partition the data into chunks and retrieve the chunk at the given index ''' return data[index*self.chunklen[0]:(index+1)*self.chunklen[0]]
[ "partition", "the", "data", "into", "chunks", "and", "retrieve", "the", "chunk", "at", "the", "given", "index" ]
thusoy/pwm
python
https://github.com/thusoy/pwm/blob/fff7d755c34f3a7235a8bf217ffa2ff5aed4926f/pwm/encoding.py#L82-L86
[ "def", "_get_chunk", "(", "self", ",", "data", ",", "index", ")", ":", "return", "data", "[", "index", "*", "self", ".", "chunklen", "[", "0", "]", ":", "(", "index", "+", "1", ")", "*", "self", ".", "chunklen", "[", "0", "]", "]" ]
fff7d755c34f3a7235a8bf217ffa2ff5aed4926f
test
memoize
Cache result of function call.
counter_robots/__init__.py
def memoize(func): """Cache result of function call.""" cache = {} @wraps(func) def inner(filename): if filename not in cache: cache[filename] = func(filename) return cache[filename] return inner
def memoize(func): """Cache result of function call.""" cache = {} @wraps(func) def inner(filename): if filename not in cache: cache[filename] = func(filename) return cache[filename] return inner
[ "Cache", "result", "of", "function", "call", "." ]
inveniosoftware/counter-robots
python
https://github.com/inveniosoftware/counter-robots/blob/484943fdc7e08f41d3ad7a9e2229afe0cec05547/counter_robots/__init__.py#L27-L36
[ "def", "memoize", "(", "func", ")", ":", "cache", "=", "{", "}", "@", "wraps", "(", "func", ")", "def", "inner", "(", "filename", ")", ":", "if", "filename", "not", "in", "cache", ":", "cache", "[", "filename", "]", "=", "func", "(", "filename", ")", "return", "cache", "[", "filename", "]", "return", "inner" ]
484943fdc7e08f41d3ad7a9e2229afe0cec05547
test
_regexp
Get a list of patterns from a file and make a regular expression.
counter_robots/__init__.py
def _regexp(filename): """Get a list of patterns from a file and make a regular expression.""" lines = _get_resource_content(filename).decode('utf-8').splitlines() return re.compile('|'.join(lines))
def _regexp(filename): """Get a list of patterns from a file and make a regular expression.""" lines = _get_resource_content(filename).decode('utf-8').splitlines() return re.compile('|'.join(lines))
[ "Get", "a", "list", "of", "patterns", "from", "a", "file", "and", "make", "a", "regular", "expression", "." ]
inveniosoftware/counter-robots
python
https://github.com/inveniosoftware/counter-robots/blob/484943fdc7e08f41d3ad7a9e2229afe0cec05547/counter_robots/__init__.py#L40-L43
[ "def", "_regexp", "(", "filename", ")", ":", "lines", "=", "_get_resource_content", "(", "filename", ")", ".", "decode", "(", "'utf-8'", ")", ".", "splitlines", "(", ")", "return", "re", ".", "compile", "(", "'|'", ".", "join", "(", "lines", ")", ")" ]
484943fdc7e08f41d3ad7a9e2229afe0cec05547
test
normalize_date_format
Dates can be defined in many ways, but zipline use aware datetime objects only. Plus, the software work with utc timezone so we convert it.
python/dna/time_utils.py
def normalize_date_format(date): ''' Dates can be defined in many ways, but zipline use aware datetime objects only. Plus, the software work with utc timezone so we convert it. ''' if isinstance(date, int): # This is probably epoch time date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(date)) # assert isinstance(date, str) or isinstance(date, unicode) if isinstance(date, str) or isinstance(date, unicode): date = dateutil.parser.parse(date) if not date.tzinfo: local_tz = pytz.timezone(_detect_timezone()) local_dt = local_tz.localize(date, is_dst=None) # TODO I'm not sure why and when I need to add a date to make it right date = local_dt.astimezone(pytz.utc) + pd.datetools.day return date
def normalize_date_format(date): ''' Dates can be defined in many ways, but zipline use aware datetime objects only. Plus, the software work with utc timezone so we convert it. ''' if isinstance(date, int): # This is probably epoch time date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(date)) # assert isinstance(date, str) or isinstance(date, unicode) if isinstance(date, str) or isinstance(date, unicode): date = dateutil.parser.parse(date) if not date.tzinfo: local_tz = pytz.timezone(_detect_timezone()) local_dt = local_tz.localize(date, is_dst=None) # TODO I'm not sure why and when I need to add a date to make it right date = local_dt.astimezone(pytz.utc) + pd.datetools.day return date
[ "Dates", "can", "be", "defined", "in", "many", "ways", "but", "zipline", "use", "aware", "datetime", "objects", "only", ".", "Plus", "the", "software", "work", "with", "utc", "timezone", "so", "we", "convert", "it", "." ]
hivetech/dna
python
https://github.com/hivetech/dna/blob/50ad00031be29765b2576fa407d35a36e0608de9/python/dna/time_utils.py#L19-L39
[ "def", "normalize_date_format", "(", "date", ")", ":", "if", "isinstance", "(", "date", ",", "int", ")", ":", "# This is probably epoch time", "date", "=", "time", ".", "strftime", "(", "'%Y-%m-%d %H:%M:%S'", ",", "time", ".", "localtime", "(", "date", ")", ")", "# assert isinstance(date, str) or isinstance(date, unicode)", "if", "isinstance", "(", "date", ",", "str", ")", "or", "isinstance", "(", "date", ",", "unicode", ")", ":", "date", "=", "dateutil", ".", "parser", ".", "parse", "(", "date", ")", "if", "not", "date", ".", "tzinfo", ":", "local_tz", "=", "pytz", ".", "timezone", "(", "_detect_timezone", "(", ")", ")", "local_dt", "=", "local_tz", ".", "localize", "(", "date", ",", "is_dst", "=", "None", ")", "# TODO I'm not sure why and when I need to add a date to make it right", "date", "=", "local_dt", ".", "astimezone", "(", "pytz", ".", "utc", ")", "+", "pd", ".", "datetools", ".", "day", "return", "date" ]
50ad00031be29765b2576fa407d35a36e0608de9
test
_detect_timezone
Get timezone as set by the system
python/dna/time_utils.py
def _detect_timezone(): ''' Get timezone as set by the system ''' default_timezone = 'America/New_York' locale_code = locale.getdefaultlocale() return default_timezone if not locale_code[0] else \ str(pytz.country_timezones[locale_code[0][-2:]][0])
def _detect_timezone(): ''' Get timezone as set by the system ''' default_timezone = 'America/New_York' locale_code = locale.getdefaultlocale() return default_timezone if not locale_code[0] else \ str(pytz.country_timezones[locale_code[0][-2:]][0])
[ "Get", "timezone", "as", "set", "by", "the", "system" ]
hivetech/dna
python
https://github.com/hivetech/dna/blob/50ad00031be29765b2576fa407d35a36e0608de9/python/dna/time_utils.py#L42-L49
[ "def", "_detect_timezone", "(", ")", ":", "default_timezone", "=", "'America/New_York'", "locale_code", "=", "locale", ".", "getdefaultlocale", "(", ")", "return", "default_timezone", "if", "not", "locale_code", "[", "0", "]", "else", "str", "(", "pytz", ".", "country_timezones", "[", "locale_code", "[", "0", "]", "[", "-", "2", ":", "]", "]", "[", "0", "]", ")" ]
50ad00031be29765b2576fa407d35a36e0608de9
test
api_url
>>> # Harmonize api endpoints >>> # __version__ should be like major.minor.fix >>> from my_app import __version__ >>> api_url(__version__, '/some/endpoint') /v0/some/endpoint
python/dna/apy/utils.py
def api_url(full_version, resource): ''' >>> # Harmonize api endpoints >>> # __version__ should be like major.minor.fix >>> from my_app import __version__ >>> api_url(__version__, '/some/endpoint') /v0/some/endpoint ''' return '/v{}/{}'.format(dna.utils.Version(full_version).major, resource)
def api_url(full_version, resource): ''' >>> # Harmonize api endpoints >>> # __version__ should be like major.minor.fix >>> from my_app import __version__ >>> api_url(__version__, '/some/endpoint') /v0/some/endpoint ''' return '/v{}/{}'.format(dna.utils.Version(full_version).major, resource)
[ ">>>", "#", "Harmonize", "api", "endpoints", ">>>", "#", "__version__", "should", "be", "like", "major", ".", "minor", ".", "fix", ">>>", "from", "my_app", "import", "__version__", ">>>", "api_url", "(", "__version__", "/", "some", "/", "endpoint", ")", "/", "v0", "/", "some", "/", "endpoint" ]
hivetech/dna
python
https://github.com/hivetech/dna/blob/50ad00031be29765b2576fa407d35a36e0608de9/python/dna/apy/utils.py#L15-L23
[ "def", "api_url", "(", "full_version", ",", "resource", ")", ":", "return", "'/v{}/{}'", ".", "format", "(", "dna", ".", "utils", ".", "Version", "(", "full_version", ")", ".", "major", ",", "resource", ")" ]
50ad00031be29765b2576fa407d35a36e0608de9
test
api_doc
>>> # Wrap api endpoints with more details >>> api_doc('/resource', secure=True, key='value') GET /resource?secure=true&key=value
python/dna/apy/utils.py
def api_doc(full_version, resource, method='GET', **kwargs): ''' >>> # Wrap api endpoints with more details >>> api_doc('/resource', secure=True, key='value') GET /resource?secure=true&key=value ''' doc = '{} {}'.format(method, api_url(full_version, resource)) params = '&'.join(['{}={}'.format(k, v) for k, v in kwargs.iteritems()]) if params: doc = '?'.join([doc, params]) return doc
def api_doc(full_version, resource, method='GET', **kwargs): ''' >>> # Wrap api endpoints with more details >>> api_doc('/resource', secure=True, key='value') GET /resource?secure=true&key=value ''' doc = '{} {}'.format(method, api_url(full_version, resource)) params = '&'.join(['{}={}'.format(k, v) for k, v in kwargs.iteritems()]) if params: doc = '?'.join([doc, params]) return doc
[ ">>>", "#", "Wrap", "api", "endpoints", "with", "more", "details", ">>>", "api_doc", "(", "/", "resource", "secure", "=", "True", "key", "=", "value", ")", "GET", "/", "resource?secure", "=", "true&key", "=", "value" ]
hivetech/dna
python
https://github.com/hivetech/dna/blob/50ad00031be29765b2576fa407d35a36e0608de9/python/dna/apy/utils.py#L26-L36
[ "def", "api_doc", "(", "full_version", ",", "resource", ",", "method", "=", "'GET'", ",", "*", "*", "kwargs", ")", ":", "doc", "=", "'{} {}'", ".", "format", "(", "method", ",", "api_url", "(", "full_version", ",", "resource", ")", ")", "params", "=", "'&'", ".", "join", "(", "[", "'{}={}'", ".", "format", "(", "k", ",", "v", ")", "for", "k", ",", "v", "in", "kwargs", ".", "iteritems", "(", ")", "]", ")", "if", "params", ":", "doc", "=", "'?'", ".", "join", "(", "[", "doc", ",", "params", "]", ")", "return", "doc" ]
50ad00031be29765b2576fa407d35a36e0608de9
test
FinancialDataStatusDatastatus.to_dict
Returns the model properties as a dict
probe/models/financial_data_status_datastatus.py
def to_dict(self): """ Returns the model properties as a dict """ result = {} for attr, _ in iteritems(self.swagger_types): value = getattr(self, attr) if isinstance(value, list): result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() else: result[attr] = value return result
def to_dict(self): """ Returns the model properties as a dict """ result = {} for attr, _ in iteritems(self.swagger_types): value = getattr(self, attr) if isinstance(value, list): result[attr] = list(map( lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value )) elif hasattr(value, "to_dict"): result[attr] = value.to_dict() else: result[attr] = value return result
[ "Returns", "the", "model", "properties", "as", "a", "dict" ]
loanzen/probe-py
python
https://github.com/loanzen/probe-py/blob/b5dbb0dba26c9b451e9bf1dec9e1aaa7f42d75a5/probe/models/financial_data_status_datastatus.py#L96-L114
[ "def", "to_dict", "(", "self", ")", ":", "result", "=", "{", "}", "for", "attr", ",", "_", "in", "iteritems", "(", "self", ".", "swagger_types", ")", ":", "value", "=", "getattr", "(", "self", ",", "attr", ")", "if", "isinstance", "(", "value", ",", "list", ")", ":", "result", "[", "attr", "]", "=", "list", "(", "map", "(", "lambda", "x", ":", "x", ".", "to_dict", "(", ")", "if", "hasattr", "(", "x", ",", "\"to_dict\"", ")", "else", "x", ",", "value", ")", ")", "elif", "hasattr", "(", "value", ",", "\"to_dict\"", ")", ":", "result", "[", "attr", "]", "=", "value", ".", "to_dict", "(", ")", "else", ":", "result", "[", "attr", "]", "=", "value", "return", "result" ]
b5dbb0dba26c9b451e9bf1dec9e1aaa7f42d75a5
test
activate_pdb_hook
Catch exceptions with a prompt for post-mortem analyzis
python/dna/debug.py
def activate_pdb_hook(): ''' Catch exceptions with a prompt for post-mortem analyzis''' def debug_exception(type_exception, value, tb): import pdb pdb.post_mortem(tb) import sys sys.excepthook = debug_exception
def activate_pdb_hook(): ''' Catch exceptions with a prompt for post-mortem analyzis''' def debug_exception(type_exception, value, tb): import pdb pdb.post_mortem(tb) import sys sys.excepthook = debug_exception
[ "Catch", "exceptions", "with", "a", "prompt", "for", "post", "-", "mortem", "analyzis" ]
hivetech/dna
python
https://github.com/hivetech/dna/blob/50ad00031be29765b2576fa407d35a36e0608de9/python/dna/debug.py#L17-L24
[ "def", "activate_pdb_hook", "(", ")", ":", "def", "debug_exception", "(", "type_exception", ",", "value", ",", "tb", ")", ":", "import", "pdb", "pdb", ".", "post_mortem", "(", "tb", ")", "import", "sys", "sys", ".", "excepthook", "=", "debug_exception" ]
50ad00031be29765b2576fa407d35a36e0608de9
test
emphasis
Clearer data printing
python/dna/debug.py
def emphasis(obj, align=True): ''' Clearer data printing ''' if isinstance(obj, dict): if align: pretty_msg = os.linesep.join( ["%25s: %s" % (k, obj[k]) for k in sorted(obj.keys())]) else: pretty_msg = json.dumps(obj, indent=4, sort_keys=True) else: return obj return pretty_msg
def emphasis(obj, align=True): ''' Clearer data printing ''' if isinstance(obj, dict): if align: pretty_msg = os.linesep.join( ["%25s: %s" % (k, obj[k]) for k in sorted(obj.keys())]) else: pretty_msg = json.dumps(obj, indent=4, sort_keys=True) else: return obj return pretty_msg
[ "Clearer", "data", "printing" ]
hivetech/dna
python
https://github.com/hivetech/dna/blob/50ad00031be29765b2576fa407d35a36e0608de9/python/dna/debug.py#L28-L38
[ "def", "emphasis", "(", "obj", ",", "align", "=", "True", ")", ":", "if", "isinstance", "(", "obj", ",", "dict", ")", ":", "if", "align", ":", "pretty_msg", "=", "os", ".", "linesep", ".", "join", "(", "[", "\"%25s: %s\"", "%", "(", "k", ",", "obj", "[", "k", "]", ")", "for", "k", "in", "sorted", "(", "obj", ".", "keys", "(", ")", ")", "]", ")", "else", ":", "pretty_msg", "=", "json", ".", "dumps", "(", "obj", ",", "indent", "=", "4", ",", "sort_keys", "=", "True", ")", "else", ":", "return", "obj", "return", "pretty_msg" ]
50ad00031be29765b2576fa407d35a36e0608de9
test
API.listcoins
Use this function to list all coins with their data which are available on cryptocoincharts. Usage: http://api.cryptocoincharts.info/listCoins
CryptoCoinChartsApi/CryptoCoinChartsApi.py
def listcoins(self): ''' Use this function to list all coins with their data which are available on cryptocoincharts. Usage: http://api.cryptocoincharts.info/listCoins ''' url = self.API_PATH + 'listCoins' json_data = json.loads(self._getdata(url)) coins = [] for entry in json_data: coin = Coin() coin.id = entry['id'] coin.name = entry['name'] coin.website = entry['website'] coin.price_btc = entry['price_btc'] coin.volume_btc = entry['volume_btc'] coins.append(coin) return coins
def listcoins(self): ''' Use this function to list all coins with their data which are available on cryptocoincharts. Usage: http://api.cryptocoincharts.info/listCoins ''' url = self.API_PATH + 'listCoins' json_data = json.loads(self._getdata(url)) coins = [] for entry in json_data: coin = Coin() coin.id = entry['id'] coin.name = entry['name'] coin.website = entry['website'] coin.price_btc = entry['price_btc'] coin.volume_btc = entry['volume_btc'] coins.append(coin) return coins
[ "Use", "this", "function", "to", "list", "all", "coins", "with", "their", "data", "which", "are", "available", "on", "cryptocoincharts", ".", "Usage", ":", "http", ":", "//", "api", ".", "cryptocoincharts", ".", "info", "/", "listCoins" ]
Dirrot/python-cryptocoincharts-api
python
https://github.com/Dirrot/python-cryptocoincharts-api/blob/8bf7a35c1032847aaea322b304014cd52853c273/CryptoCoinChartsApi/CryptoCoinChartsApi.py#L23-L42
[ "def", "listcoins", "(", "self", ")", ":", "url", "=", "self", ".", "API_PATH", "+", "'listCoins'", "json_data", "=", "json", ".", "loads", "(", "self", ".", "_getdata", "(", "url", ")", ")", "coins", "=", "[", "]", "for", "entry", "in", "json_data", ":", "coin", "=", "Coin", "(", ")", "coin", ".", "id", "=", "entry", "[", "'id'", "]", "coin", ".", "name", "=", "entry", "[", "'name'", "]", "coin", ".", "website", "=", "entry", "[", "'website'", "]", "coin", ".", "price_btc", "=", "entry", "[", "'price_btc'", "]", "coin", ".", "volume_btc", "=", "entry", "[", "'volume_btc'", "]", "coins", ".", "append", "(", "coin", ")", "return", "coins" ]
8bf7a35c1032847aaea322b304014cd52853c273
test
API.tradingpair
Use this function to query price and volume data for ONE trading pair. A list with all coin currencies can be found by using the listcoins method. A example pair: currency1_currency2 = "doge_btc" Usage: http://api.cryptocoincharts.info/tradingPair/[currency1_currency2]
CryptoCoinChartsApi/CryptoCoinChartsApi.py
def tradingpair(self, pair): ''' Use this function to query price and volume data for ONE trading pair. A list with all coin currencies can be found by using the listcoins method. A example pair: currency1_currency2 = "doge_btc" Usage: http://api.cryptocoincharts.info/tradingPair/[currency1_currency2] ''' url = self.API_PATH + 'tradingPair/' + pair json_data = json.loads(self._getdata(url)) tradingpair = TradingPair() tradingpair.id = json_data['id'] tradingpair.price = json_data['price'] tradingpair.price_before_24h = json_data['price_before_24h'] tradingpair.volume_first = json_data['volume_first'] tradingpair.volume_second = json_data['volume_second'] tradingpair.volume_btc = json_data['volume_btc'] tradingpair.best_market = json_data['best_market'] tradingpair.latest_trade = json_data['latest_trade'] return tradingpair
def tradingpair(self, pair): ''' Use this function to query price and volume data for ONE trading pair. A list with all coin currencies can be found by using the listcoins method. A example pair: currency1_currency2 = "doge_btc" Usage: http://api.cryptocoincharts.info/tradingPair/[currency1_currency2] ''' url = self.API_PATH + 'tradingPair/' + pair json_data = json.loads(self._getdata(url)) tradingpair = TradingPair() tradingpair.id = json_data['id'] tradingpair.price = json_data['price'] tradingpair.price_before_24h = json_data['price_before_24h'] tradingpair.volume_first = json_data['volume_first'] tradingpair.volume_second = json_data['volume_second'] tradingpair.volume_btc = json_data['volume_btc'] tradingpair.best_market = json_data['best_market'] tradingpair.latest_trade = json_data['latest_trade'] return tradingpair
[ "Use", "this", "function", "to", "query", "price", "and", "volume", "data", "for", "ONE", "trading", "pair", ".", "A", "list", "with", "all", "coin", "currencies", "can", "be", "found", "by", "using", "the", "listcoins", "method", ".", "A", "example", "pair", ":", "currency1_currency2", "=", "doge_btc", "Usage", ":", "http", ":", "//", "api", ".", "cryptocoincharts", ".", "info", "/", "tradingPair", "/", "[", "currency1_currency2", "]" ]
Dirrot/python-cryptocoincharts-api
python
https://github.com/Dirrot/python-cryptocoincharts-api/blob/8bf7a35c1032847aaea322b304014cd52853c273/CryptoCoinChartsApi/CryptoCoinChartsApi.py#L44-L65
[ "def", "tradingpair", "(", "self", ",", "pair", ")", ":", "url", "=", "self", ".", "API_PATH", "+", "'tradingPair/'", "+", "pair", "json_data", "=", "json", ".", "loads", "(", "self", ".", "_getdata", "(", "url", ")", ")", "tradingpair", "=", "TradingPair", "(", ")", "tradingpair", ".", "id", "=", "json_data", "[", "'id'", "]", "tradingpair", ".", "price", "=", "json_data", "[", "'price'", "]", "tradingpair", ".", "price_before_24h", "=", "json_data", "[", "'price_before_24h'", "]", "tradingpair", ".", "volume_first", "=", "json_data", "[", "'volume_first'", "]", "tradingpair", ".", "volume_second", "=", "json_data", "[", "'volume_second'", "]", "tradingpair", ".", "volume_btc", "=", "json_data", "[", "'volume_btc'", "]", "tradingpair", ".", "best_market", "=", "json_data", "[", "'best_market'", "]", "tradingpair", ".", "latest_trade", "=", "json_data", "[", "'latest_trade'", "]", "return", "tradingpair" ]
8bf7a35c1032847aaea322b304014cd52853c273
test
API.tradingpairs
Use this function to query price and volume data for MANY trading pairs. Usage: http://api.cryptocoincharts.info/tradingPairs/[currency1_currency2,currency2_currency3,...] A example pair: currency1_currency2 = "doge_btc" currency2_currency3 = "btc_eur" http://api.cryptocoincharts.info/tradingPairs/"doge_btc,btc_eur"
CryptoCoinChartsApi/CryptoCoinChartsApi.py
def tradingpairs(self, pairs): ''' Use this function to query price and volume data for MANY trading pairs. Usage: http://api.cryptocoincharts.info/tradingPairs/[currency1_currency2,currency2_currency3,...] A example pair: currency1_currency2 = "doge_btc" currency2_currency3 = "btc_eur" http://api.cryptocoincharts.info/tradingPairs/"doge_btc,btc_eur" ''' url = self.API_PATH + 'tradingPairs/' data = { 'pairs':pairs } json_data = json.loads(self._getdata(url, data)) tradingpairs = [] for entry in json_data: tradingpair = TradingPair() tradingpair.id = entry['id'] tradingpair.price = entry['price'] tradingpair.price_before_24h = entry['price_before_24h'] tradingpair.volume_first = entry['volume_first'] tradingpair.volume_second = entry['volume_second'] tradingpair.volume_btc = entry['volume_btc'] tradingpair.best_market = entry['best_market'] tradingpair.latest_trade = entry['latest_trade'] tradingpairs.append(tradingpair) return tradingpairs
def tradingpairs(self, pairs): ''' Use this function to query price and volume data for MANY trading pairs. Usage: http://api.cryptocoincharts.info/tradingPairs/[currency1_currency2,currency2_currency3,...] A example pair: currency1_currency2 = "doge_btc" currency2_currency3 = "btc_eur" http://api.cryptocoincharts.info/tradingPairs/"doge_btc,btc_eur" ''' url = self.API_PATH + 'tradingPairs/' data = { 'pairs':pairs } json_data = json.loads(self._getdata(url, data)) tradingpairs = [] for entry in json_data: tradingpair = TradingPair() tradingpair.id = entry['id'] tradingpair.price = entry['price'] tradingpair.price_before_24h = entry['price_before_24h'] tradingpair.volume_first = entry['volume_first'] tradingpair.volume_second = entry['volume_second'] tradingpair.volume_btc = entry['volume_btc'] tradingpair.best_market = entry['best_market'] tradingpair.latest_trade = entry['latest_trade'] tradingpairs.append(tradingpair) return tradingpairs
[ "Use", "this", "function", "to", "query", "price", "and", "volume", "data", "for", "MANY", "trading", "pairs", ".", "Usage", ":", "http", ":", "//", "api", ".", "cryptocoincharts", ".", "info", "/", "tradingPairs", "/", "[", "currency1_currency2", "currency2_currency3", "...", "]", "A", "example", "pair", ":", "currency1_currency2", "=", "doge_btc", "currency2_currency3", "=", "btc_eur", "http", ":", "//", "api", ".", "cryptocoincharts", ".", "info", "/", "tradingPairs", "/", "doge_btc", "btc_eur" ]
Dirrot/python-cryptocoincharts-api
python
https://github.com/Dirrot/python-cryptocoincharts-api/blob/8bf7a35c1032847aaea322b304014cd52853c273/CryptoCoinChartsApi/CryptoCoinChartsApi.py#L67-L93
[ "def", "tradingpairs", "(", "self", ",", "pairs", ")", ":", "url", "=", "self", ".", "API_PATH", "+", "'tradingPairs/'", "data", "=", "{", "'pairs'", ":", "pairs", "}", "json_data", "=", "json", ".", "loads", "(", "self", ".", "_getdata", "(", "url", ",", "data", ")", ")", "tradingpairs", "=", "[", "]", "for", "entry", "in", "json_data", ":", "tradingpair", "=", "TradingPair", "(", ")", "tradingpair", ".", "id", "=", "entry", "[", "'id'", "]", "tradingpair", ".", "price", "=", "entry", "[", "'price'", "]", "tradingpair", ".", "price_before_24h", "=", "entry", "[", "'price_before_24h'", "]", "tradingpair", ".", "volume_first", "=", "entry", "[", "'volume_first'", "]", "tradingpair", ".", "volume_second", "=", "entry", "[", "'volume_second'", "]", "tradingpair", ".", "volume_btc", "=", "entry", "[", "'volume_btc'", "]", "tradingpair", ".", "best_market", "=", "entry", "[", "'best_market'", "]", "tradingpair", ".", "latest_trade", "=", "entry", "[", "'latest_trade'", "]", "tradingpairs", ".", "append", "(", "tradingpair", ")", "return", "tradingpairs" ]
8bf7a35c1032847aaea322b304014cd52853c273
test
API._getdata
Wrapper method
CryptoCoinChartsApi/CryptoCoinChartsApi.py
def _getdata(self, url, data = ""): ''' Wrapper method ''' request = Request(url) if data != "": request = Request(url, urlencode(data)) try: response = urlopen(request) except HTTPError as e: print('The Server couldn\'t fulfill the request.') print('Error code: ', e.code) except URLError as e: print('We failed to reach a server.') print('Reason: ', e.code) else: # Everything is fine. return response.read()
def _getdata(self, url, data = ""): ''' Wrapper method ''' request = Request(url) if data != "": request = Request(url, urlencode(data)) try: response = urlopen(request) except HTTPError as e: print('The Server couldn\'t fulfill the request.') print('Error code: ', e.code) except URLError as e: print('We failed to reach a server.') print('Reason: ', e.code) else: # Everything is fine. return response.read()
[ "Wrapper", "method" ]
Dirrot/python-cryptocoincharts-api
python
https://github.com/Dirrot/python-cryptocoincharts-api/blob/8bf7a35c1032847aaea322b304014cd52853c273/CryptoCoinChartsApi/CryptoCoinChartsApi.py#L98-L117
[ "def", "_getdata", "(", "self", ",", "url", ",", "data", "=", "\"\"", ")", ":", "request", "=", "Request", "(", "url", ")", "if", "data", "!=", "\"\"", ":", "request", "=", "Request", "(", "url", ",", "urlencode", "(", "data", ")", ")", "try", ":", "response", "=", "urlopen", "(", "request", ")", "except", "HTTPError", "as", "e", ":", "print", "(", "'The Server couldn\\'t fulfill the request.'", ")", "print", "(", "'Error code: '", ",", "e", ".", "code", ")", "except", "URLError", "as", "e", ":", "print", "(", "'We failed to reach a server.'", ")", "print", "(", "'Reason: '", ",", "e", ".", "code", ")", "else", ":", "# Everything is fine.", "return", "response", ".", "read", "(", ")" ]
8bf7a35c1032847aaea322b304014cd52853c273
test
handle_jobs
Connects to the remote master and continuously receives calls, executes them, then returns a response until interrupted.
highfive/worker.py
async def handle_jobs(job_handler, host, port, *, loop): """ Connects to the remote master and continuously receives calls, executes them, then returns a response until interrupted. """ try: try: reader, writer = await asyncio.open_connection(host, port, loop=loop) except OSError: logging.error("worker could not connect to server") return while True: try: call_encoded = await reader.readuntil(b"\n") except (asyncio.IncompleteReadError, ConnectionResetError): break logging.debug("worker got call") call_json = call_encoded.decode("utf-8") call = json.loads(call_json) response = job_handler(call) response_json = json.dumps(response) + "\n" response_encoded = response_json.encode("utf-8") writer.write(response_encoded) logging.debug("worker returned response") except KeyboardInterrupt: pass
async def handle_jobs(job_handler, host, port, *, loop): """ Connects to the remote master and continuously receives calls, executes them, then returns a response until interrupted. """ try: try: reader, writer = await asyncio.open_connection(host, port, loop=loop) except OSError: logging.error("worker could not connect to server") return while True: try: call_encoded = await reader.readuntil(b"\n") except (asyncio.IncompleteReadError, ConnectionResetError): break logging.debug("worker got call") call_json = call_encoded.decode("utf-8") call = json.loads(call_json) response = job_handler(call) response_json = json.dumps(response) + "\n" response_encoded = response_json.encode("utf-8") writer.write(response_encoded) logging.debug("worker returned response") except KeyboardInterrupt: pass
[ "Connects", "to", "the", "remote", "master", "and", "continuously", "receives", "calls", "executes", "them", "then", "returns", "a", "response", "until", "interrupted", "." ]
abau171/highfive
python
https://github.com/abau171/highfive/blob/07b3829331072035ab100d1d66deca3e8f3f372a/highfive/worker.py#L10-L43
[ "async", "def", "handle_jobs", "(", "job_handler", ",", "host", ",", "port", ",", "*", ",", "loop", ")", ":", "try", ":", "try", ":", "reader", ",", "writer", "=", "await", "asyncio", ".", "open_connection", "(", "host", ",", "port", ",", "loop", "=", "loop", ")", "except", "OSError", ":", "logging", ".", "error", "(", "\"worker could not connect to server\"", ")", "return", "while", "True", ":", "try", ":", "call_encoded", "=", "await", "reader", ".", "readuntil", "(", "b\"\\n\"", ")", "except", "(", "asyncio", ".", "IncompleteReadError", ",", "ConnectionResetError", ")", ":", "break", "logging", ".", "debug", "(", "\"worker got call\"", ")", "call_json", "=", "call_encoded", ".", "decode", "(", "\"utf-8\"", ")", "call", "=", "json", ".", "loads", "(", "call_json", ")", "response", "=", "job_handler", "(", "call", ")", "response_json", "=", "json", ".", "dumps", "(", "response", ")", "+", "\"\\n\"", "response_encoded", "=", "response_json", ".", "encode", "(", "\"utf-8\"", ")", "writer", ".", "write", "(", "response_encoded", ")", "logging", ".", "debug", "(", "\"worker returned response\"", ")", "except", "KeyboardInterrupt", ":", "pass" ]
07b3829331072035ab100d1d66deca3e8f3f372a
test
worker_main
Starts an asyncio event loop to connect to the master and run jobs.
highfive/worker.py
def worker_main(job_handler, host, port): """ Starts an asyncio event loop to connect to the master and run jobs. """ loop = asyncio.new_event_loop() asyncio.set_event_loop(None) loop.run_until_complete(handle_jobs(job_handler, host, port, loop=loop)) loop.close()
def worker_main(job_handler, host, port): """ Starts an asyncio event loop to connect to the master and run jobs. """ loop = asyncio.new_event_loop() asyncio.set_event_loop(None) loop.run_until_complete(handle_jobs(job_handler, host, port, loop=loop)) loop.close()
[ "Starts", "an", "asyncio", "event", "loop", "to", "connect", "to", "the", "master", "and", "run", "jobs", "." ]
abau171/highfive
python
https://github.com/abau171/highfive/blob/07b3829331072035ab100d1d66deca3e8f3f372a/highfive/worker.py#L46-L54
[ "def", "worker_main", "(", "job_handler", ",", "host", ",", "port", ")", ":", "loop", "=", "asyncio", ".", "new_event_loop", "(", ")", "asyncio", ".", "set_event_loop", "(", "None", ")", "loop", ".", "run_until_complete", "(", "handle_jobs", "(", "job_handler", ",", "host", ",", "port", ",", "loop", "=", "loop", ")", ")", "loop", ".", "close", "(", ")" ]
07b3829331072035ab100d1d66deca3e8f3f372a
test
run_worker_pool
Runs a pool of workers which connect to a remote HighFive master and begin executing calls.
highfive/worker.py
def run_worker_pool(job_handler, host="localhost", port=48484, *, max_workers=None): """ Runs a pool of workers which connect to a remote HighFive master and begin executing calls. """ if max_workers is None: max_workers = multiprocessing.cpu_count() processes = [] for _ in range(max_workers): p = multiprocessing.Process(target=worker_main, args=(job_handler, host, port)) p.start() processes.append(p) logger.debug("workers started") for p in processes: p.join() logger.debug("all workers completed")
def run_worker_pool(job_handler, host="localhost", port=48484, *, max_workers=None): """ Runs a pool of workers which connect to a remote HighFive master and begin executing calls. """ if max_workers is None: max_workers = multiprocessing.cpu_count() processes = [] for _ in range(max_workers): p = multiprocessing.Process(target=worker_main, args=(job_handler, host, port)) p.start() processes.append(p) logger.debug("workers started") for p in processes: p.join() logger.debug("all workers completed")
[ "Runs", "a", "pool", "of", "workers", "which", "connect", "to", "a", "remote", "HighFive", "master", "and", "begin", "executing", "calls", "." ]
abau171/highfive
python
https://github.com/abau171/highfive/blob/07b3829331072035ab100d1d66deca3e8f3f372a/highfive/worker.py#L57-L79
[ "def", "run_worker_pool", "(", "job_handler", ",", "host", "=", "\"localhost\"", ",", "port", "=", "48484", ",", "*", ",", "max_workers", "=", "None", ")", ":", "if", "max_workers", "is", "None", ":", "max_workers", "=", "multiprocessing", ".", "cpu_count", "(", ")", "processes", "=", "[", "]", "for", "_", "in", "range", "(", "max_workers", ")", ":", "p", "=", "multiprocessing", ".", "Process", "(", "target", "=", "worker_main", ",", "args", "=", "(", "job_handler", ",", "host", ",", "port", ")", ")", "p", ".", "start", "(", ")", "processes", ".", "append", "(", "p", ")", "logger", ".", "debug", "(", "\"workers started\"", ")", "for", "p", "in", "processes", ":", "p", ".", "join", "(", ")", "logger", ".", "debug", "(", "\"all workers completed\"", ")" ]
07b3829331072035ab100d1d66deca3e8f3f372a
test
CompanyDetailCompany.classification
Sets the classification of this CompanyDetailCompany. Classification of Company :param classification: The classification of this CompanyDetailCompany. :type: str
probe/models/company_detail_company.py
def classification(self, classification): """ Sets the classification of this CompanyDetailCompany. Classification of Company :param classification: The classification of this CompanyDetailCompany. :type: str """ allowed_values = ["Public Limited Indian Non-Government Company", "Private Limited Indian Non-Government Company", "One Person Company", "Private Limited Foreign Company Incorporated in India", "Public Limited Foreign Company Incorporated in India", "Union Government Company", "State Government Company", "Guarantee & Association Public", "Guarantee & Association Private", "Not For Profit Company", "Unlimited Liabilities Public", "Unlimited Liabilities Private", "Undefined"] if classification not in allowed_values: raise ValueError( "Invalid value for `classification`, must be one of {0}" .format(allowed_values) ) self._classification = classification
def classification(self, classification): """ Sets the classification of this CompanyDetailCompany. Classification of Company :param classification: The classification of this CompanyDetailCompany. :type: str """ allowed_values = ["Public Limited Indian Non-Government Company", "Private Limited Indian Non-Government Company", "One Person Company", "Private Limited Foreign Company Incorporated in India", "Public Limited Foreign Company Incorporated in India", "Union Government Company", "State Government Company", "Guarantee & Association Public", "Guarantee & Association Private", "Not For Profit Company", "Unlimited Liabilities Public", "Unlimited Liabilities Private", "Undefined"] if classification not in allowed_values: raise ValueError( "Invalid value for `classification`, must be one of {0}" .format(allowed_values) ) self._classification = classification
[ "Sets", "the", "classification", "of", "this", "CompanyDetailCompany", ".", "Classification", "of", "Company" ]
loanzen/probe-py
python
https://github.com/loanzen/probe-py/blob/b5dbb0dba26c9b451e9bf1dec9e1aaa7f42d75a5/probe/models/company_detail_company.py#L182-L196
[ "def", "classification", "(", "self", ",", "classification", ")", ":", "allowed_values", "=", "[", "\"Public Limited Indian Non-Government Company\"", ",", "\"Private Limited Indian Non-Government Company\"", ",", "\"One Person Company\"", ",", "\"Private Limited Foreign Company Incorporated in India\"", ",", "\"Public Limited Foreign Company Incorporated in India\"", ",", "\"Union Government Company\"", ",", "\"State Government Company\"", ",", "\"Guarantee & Association Public\"", ",", "\"Guarantee & Association Private\"", ",", "\"Not For Profit Company\"", ",", "\"Unlimited Liabilities Public\"", ",", "\"Unlimited Liabilities Private\"", ",", "\"Undefined\"", "]", "if", "classification", "not", "in", "allowed_values", ":", "raise", "ValueError", "(", "\"Invalid value for `classification`, must be one of {0}\"", ".", "format", "(", "allowed_values", ")", ")", "self", ".", "_classification", "=", "classification" ]
b5dbb0dba26c9b451e9bf1dec9e1aaa7f42d75a5
test
LWLink._send_message
Add message to queue and start processing the queue.
lightwave/lightwave.py
def _send_message(self, msg): """Add message to queue and start processing the queue.""" LWLink.the_queue.put_nowait(msg) if LWLink.thread is None or not LWLink.thread.isAlive(): LWLink.thread = Thread(target=self._send_queue) LWLink.thread.start()
def _send_message(self, msg): """Add message to queue and start processing the queue.""" LWLink.the_queue.put_nowait(msg) if LWLink.thread is None or not LWLink.thread.isAlive(): LWLink.thread = Thread(target=self._send_queue) LWLink.thread.start()
[ "Add", "message", "to", "queue", "and", "start", "processing", "the", "queue", "." ]
GeoffAtHome/lightwave
python
https://github.com/GeoffAtHome/lightwave/blob/2fab4ee8c9f14dd97dffd4b8cd70b217e884e581/lightwave/lightwave.py#L29-L34
[ "def", "_send_message", "(", "self", ",", "msg", ")", ":", "LWLink", ".", "the_queue", ".", "put_nowait", "(", "msg", ")", "if", "LWLink", ".", "thread", "is", "None", "or", "not", "LWLink", ".", "thread", ".", "isAlive", "(", ")", ":", "LWLink", ".", "thread", "=", "Thread", "(", "target", "=", "self", ".", "_send_queue", ")", "LWLink", ".", "thread", ".", "start", "(", ")" ]
2fab4ee8c9f14dd97dffd4b8cd70b217e884e581
test
LWLink.turn_on_light
Create the message to turn light on.
lightwave/lightwave.py
def turn_on_light(self, device_id, name): """Create the message to turn light on.""" msg = "!%sFdP32|Turn On|%s" % (device_id, name) self._send_message(msg)
def turn_on_light(self, device_id, name): """Create the message to turn light on.""" msg = "!%sFdP32|Turn On|%s" % (device_id, name) self._send_message(msg)
[ "Create", "the", "message", "to", "turn", "light", "on", "." ]
GeoffAtHome/lightwave
python
https://github.com/GeoffAtHome/lightwave/blob/2fab4ee8c9f14dd97dffd4b8cd70b217e884e581/lightwave/lightwave.py#L46-L49
[ "def", "turn_on_light", "(", "self", ",", "device_id", ",", "name", ")", ":", "msg", "=", "\"!%sFdP32|Turn On|%s\"", "%", "(", "device_id", ",", "name", ")", "self", ".", "_send_message", "(", "msg", ")" ]
2fab4ee8c9f14dd97dffd4b8cd70b217e884e581
test
LWLink.turn_on_switch
Create the message to turn switch on.
lightwave/lightwave.py
def turn_on_switch(self, device_id, name): """Create the message to turn switch on.""" msg = "!%sF1|Turn On|%s" % (device_id, name) self._send_message(msg)
def turn_on_switch(self, device_id, name): """Create the message to turn switch on.""" msg = "!%sF1|Turn On|%s" % (device_id, name) self._send_message(msg)
[ "Create", "the", "message", "to", "turn", "switch", "on", "." ]
GeoffAtHome/lightwave
python
https://github.com/GeoffAtHome/lightwave/blob/2fab4ee8c9f14dd97dffd4b8cd70b217e884e581/lightwave/lightwave.py#L51-L54
[ "def", "turn_on_switch", "(", "self", ",", "device_id", ",", "name", ")", ":", "msg", "=", "\"!%sF1|Turn On|%s\"", "%", "(", "device_id", ",", "name", ")", "self", ".", "_send_message", "(", "msg", ")" ]
2fab4ee8c9f14dd97dffd4b8cd70b217e884e581
test
LWLink.turn_on_with_brightness
Scale brightness from 0..255 to 1..32.
lightwave/lightwave.py
def turn_on_with_brightness(self, device_id, name, brightness): """Scale brightness from 0..255 to 1..32.""" brightness_value = round((brightness * 31) / 255) + 1 # F1 = Light on and F0 = light off. FdP[0..32] is brightness. 32 is # full. We want that when turning the light on. msg = "!%sFdP%d|Lights %d|%s" % ( device_id, brightness_value, brightness_value, name) self._send_message(msg)
def turn_on_with_brightness(self, device_id, name, brightness): """Scale brightness from 0..255 to 1..32.""" brightness_value = round((brightness * 31) / 255) + 1 # F1 = Light on and F0 = light off. FdP[0..32] is brightness. 32 is # full. We want that when turning the light on. msg = "!%sFdP%d|Lights %d|%s" % ( device_id, brightness_value, brightness_value, name) self._send_message(msg)
[ "Scale", "brightness", "from", "0", "..", "255", "to", "1", "..", "32", "." ]
GeoffAtHome/lightwave
python
https://github.com/GeoffAtHome/lightwave/blob/2fab4ee8c9f14dd97dffd4b8cd70b217e884e581/lightwave/lightwave.py#L56-L63
[ "def", "turn_on_with_brightness", "(", "self", ",", "device_id", ",", "name", ",", "brightness", ")", ":", "brightness_value", "=", "round", "(", "(", "brightness", "*", "31", ")", "/", "255", ")", "+", "1", "# F1 = Light on and F0 = light off. FdP[0..32] is brightness. 32 is", "# full. We want that when turning the light on.", "msg", "=", "\"!%sFdP%d|Lights %d|%s\"", "%", "(", "device_id", ",", "brightness_value", ",", "brightness_value", ",", "name", ")", "self", ".", "_send_message", "(", "msg", ")" ]
2fab4ee8c9f14dd97dffd4b8cd70b217e884e581
test
LWLink.turn_off
Create the message to turn light or switch off.
lightwave/lightwave.py
def turn_off(self, device_id, name): """Create the message to turn light or switch off.""" msg = "!%sF0|Turn Off|%s" % (device_id, name) self._send_message(msg)
def turn_off(self, device_id, name): """Create the message to turn light or switch off.""" msg = "!%sF0|Turn Off|%s" % (device_id, name) self._send_message(msg)
[ "Create", "the", "message", "to", "turn", "light", "or", "switch", "off", "." ]
GeoffAtHome/lightwave
python
https://github.com/GeoffAtHome/lightwave/blob/2fab4ee8c9f14dd97dffd4b8cd70b217e884e581/lightwave/lightwave.py#L65-L68
[ "def", "turn_off", "(", "self", ",", "device_id", ",", "name", ")", ":", "msg", "=", "\"!%sF0|Turn Off|%s\"", "%", "(", "device_id", ",", "name", ")", "self", ".", "_send_message", "(", "msg", ")" ]
2fab4ee8c9f14dd97dffd4b8cd70b217e884e581
test
LWLink._send_queue
If the queue is not empty, process the queue.
lightwave/lightwave.py
def _send_queue(self): """If the queue is not empty, process the queue.""" while not LWLink.the_queue.empty(): self._send_reliable_message(LWLink.the_queue.get_nowait())
def _send_queue(self): """If the queue is not empty, process the queue.""" while not LWLink.the_queue.empty(): self._send_reliable_message(LWLink.the_queue.get_nowait())
[ "If", "the", "queue", "is", "not", "empty", "process", "the", "queue", "." ]
GeoffAtHome/lightwave
python
https://github.com/GeoffAtHome/lightwave/blob/2fab4ee8c9f14dd97dffd4b8cd70b217e884e581/lightwave/lightwave.py#L70-L73
[ "def", "_send_queue", "(", "self", ")", ":", "while", "not", "LWLink", ".", "the_queue", ".", "empty", "(", ")", ":", "self", ".", "_send_reliable_message", "(", "LWLink", ".", "the_queue", ".", "get_nowait", "(", ")", ")" ]
2fab4ee8c9f14dd97dffd4b8cd70b217e884e581
test
LWLink._send_reliable_message
Send msg to LightwaveRF hub.
lightwave/lightwave.py
def _send_reliable_message(self, msg): """Send msg to LightwaveRF hub.""" result = False max_retries = 15 trans_id = next(LWLink.transaction_id) msg = "%d,%s" % (trans_id, msg) try: with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) \ as write_sock, \ socket.socket(socket.AF_INET, socket.SOCK_DGRAM) \ as read_sock: write_sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) read_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) read_sock.settimeout(self.SOCKET_TIMEOUT) read_sock.bind(('0.0.0.0', self.RX_PORT)) while max_retries: max_retries -= 1 write_sock.sendto(msg.encode( 'UTF-8'), (LWLink.link_ip, self.TX_PORT)) result = False while True: response, dummy = read_sock.recvfrom(1024) response = response.decode('UTF-8') if "Not yet registered." in response: _LOGGER.error("Not yet registered") self.register() result = True break if response.startswith("%d,OK" % trans_id): result = True break if response.startswith("%d,ERR" % trans_id): _LOGGER.error(response) break _LOGGER.info(response) if result: break time.sleep(0.25) except socket.timeout: _LOGGER.error("LW broker timeout!") return result except Exception as ex: _LOGGER.error(ex) raise if result: _LOGGER.info("LW broker OK!") else: _LOGGER.error("LW broker fail!") return result
def _send_reliable_message(self, msg): """Send msg to LightwaveRF hub.""" result = False max_retries = 15 trans_id = next(LWLink.transaction_id) msg = "%d,%s" % (trans_id, msg) try: with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) \ as write_sock, \ socket.socket(socket.AF_INET, socket.SOCK_DGRAM) \ as read_sock: write_sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) read_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) read_sock.settimeout(self.SOCKET_TIMEOUT) read_sock.bind(('0.0.0.0', self.RX_PORT)) while max_retries: max_retries -= 1 write_sock.sendto(msg.encode( 'UTF-8'), (LWLink.link_ip, self.TX_PORT)) result = False while True: response, dummy = read_sock.recvfrom(1024) response = response.decode('UTF-8') if "Not yet registered." in response: _LOGGER.error("Not yet registered") self.register() result = True break if response.startswith("%d,OK" % trans_id): result = True break if response.startswith("%d,ERR" % trans_id): _LOGGER.error(response) break _LOGGER.info(response) if result: break time.sleep(0.25) except socket.timeout: _LOGGER.error("LW broker timeout!") return result except Exception as ex: _LOGGER.error(ex) raise if result: _LOGGER.info("LW broker OK!") else: _LOGGER.error("LW broker fail!") return result
[ "Send", "msg", "to", "LightwaveRF", "hub", "." ]
GeoffAtHome/lightwave
python
https://github.com/GeoffAtHome/lightwave/blob/2fab4ee8c9f14dd97dffd4b8cd70b217e884e581/lightwave/lightwave.py#L75-L132
[ "def", "_send_reliable_message", "(", "self", ",", "msg", ")", ":", "result", "=", "False", "max_retries", "=", "15", "trans_id", "=", "next", "(", "LWLink", ".", "transaction_id", ")", "msg", "=", "\"%d,%s\"", "%", "(", "trans_id", ",", "msg", ")", "try", ":", "with", "socket", ".", "socket", "(", "socket", ".", "AF_INET", ",", "socket", ".", "SOCK_DGRAM", ")", "as", "write_sock", ",", "socket", ".", "socket", "(", "socket", ".", "AF_INET", ",", "socket", ".", "SOCK_DGRAM", ")", "as", "read_sock", ":", "write_sock", ".", "setsockopt", "(", "socket", ".", "SOL_SOCKET", ",", "socket", ".", "SO_REUSEADDR", ",", "1", ")", "read_sock", ".", "setsockopt", "(", "socket", ".", "SOL_SOCKET", ",", "socket", ".", "SO_BROADCAST", ",", "1", ")", "read_sock", ".", "settimeout", "(", "self", ".", "SOCKET_TIMEOUT", ")", "read_sock", ".", "bind", "(", "(", "'0.0.0.0'", ",", "self", ".", "RX_PORT", ")", ")", "while", "max_retries", ":", "max_retries", "-=", "1", "write_sock", ".", "sendto", "(", "msg", ".", "encode", "(", "'UTF-8'", ")", ",", "(", "LWLink", ".", "link_ip", ",", "self", ".", "TX_PORT", ")", ")", "result", "=", "False", "while", "True", ":", "response", ",", "dummy", "=", "read_sock", ".", "recvfrom", "(", "1024", ")", "response", "=", "response", ".", "decode", "(", "'UTF-8'", ")", "if", "\"Not yet registered.\"", "in", "response", ":", "_LOGGER", ".", "error", "(", "\"Not yet registered\"", ")", "self", ".", "register", "(", ")", "result", "=", "True", "break", "if", "response", ".", "startswith", "(", "\"%d,OK\"", "%", "trans_id", ")", ":", "result", "=", "True", "break", "if", "response", ".", "startswith", "(", "\"%d,ERR\"", "%", "trans_id", ")", ":", "_LOGGER", ".", "error", "(", "response", ")", "break", "_LOGGER", ".", "info", "(", "response", ")", "if", "result", ":", "break", "time", ".", "sleep", "(", "0.25", ")", "except", "socket", ".", "timeout", ":", "_LOGGER", ".", "error", "(", "\"LW broker timeout!\"", ")", "return", "result", "except", "Exception", "as", "ex", ":", "_LOGGER", ".", "error", "(", "ex", ")", "raise", "if", "result", ":", "_LOGGER", ".", "info", "(", "\"LW broker OK!\"", ")", "else", ":", "_LOGGER", ".", "error", "(", "\"LW broker fail!\"", ")", "return", "result" ]
2fab4ee8c9f14dd97dffd4b8cd70b217e884e581
test
create_adapter
Generates a wrapped adapter for the given object Parameters ---------- obj : list, buffer, array, or file Raises ------ ValueError If presented with an object that cannot be adapted Returns ------- CMPH capable adapter
cmph/_adapters.py
def create_adapter(cmph, ffi, obj): """ Generates a wrapped adapter for the given object Parameters ---------- obj : list, buffer, array, or file Raises ------ ValueError If presented with an object that cannot be adapted Returns ------- CMPH capable adapter """ # if arraylike and fixed unit size # if file # if buffer if is_file_location(obj): # The FP is captured for GC reasons inside the dtor closure # pylint: disable=invalid-name fd = open(obj) adapter = cmph.cmph_io_nlfile_adapter(fd) def dtor(): cmph.cmph_io_nlfile_adapter_destroy(adapter) fd.close() # pylint: enable=invalid-name return _AdapterCxt(adapter, dtor) elif is_file(obj): adapter = cmph.cmph_io_nlfile_adapter(obj) dtor = lambda: cmph.cmph_io_nlfile_adapter_destroy(adapter) return _AdapterCxt(adapter, dtor) elif isinstance(obj, Sequence): if len(obj) == 0: raise ValueError("An empty sequence is already a perfect hash!") return _create_pyobj_adapter(cmph, ffi, obj) else: raise ValueError("data cannot have a cmph wrapper generated")
def create_adapter(cmph, ffi, obj): """ Generates a wrapped adapter for the given object Parameters ---------- obj : list, buffer, array, or file Raises ------ ValueError If presented with an object that cannot be adapted Returns ------- CMPH capable adapter """ # if arraylike and fixed unit size # if file # if buffer if is_file_location(obj): # The FP is captured for GC reasons inside the dtor closure # pylint: disable=invalid-name fd = open(obj) adapter = cmph.cmph_io_nlfile_adapter(fd) def dtor(): cmph.cmph_io_nlfile_adapter_destroy(adapter) fd.close() # pylint: enable=invalid-name return _AdapterCxt(adapter, dtor) elif is_file(obj): adapter = cmph.cmph_io_nlfile_adapter(obj) dtor = lambda: cmph.cmph_io_nlfile_adapter_destroy(adapter) return _AdapterCxt(adapter, dtor) elif isinstance(obj, Sequence): if len(obj) == 0: raise ValueError("An empty sequence is already a perfect hash!") return _create_pyobj_adapter(cmph, ffi, obj) else: raise ValueError("data cannot have a cmph wrapper generated")
[ "Generates", "a", "wrapped", "adapter", "for", "the", "given", "object" ]
URXtech/cmph-cffi
python
https://github.com/URXtech/cmph-cffi/blob/85298572e51675cd0c7ef1052ed9989b0e57f0cc/cmph/_adapters.py#L99-L141
[ "def", "create_adapter", "(", "cmph", ",", "ffi", ",", "obj", ")", ":", "# if arraylike and fixed unit size", "# if file", "# if buffer", "if", "is_file_location", "(", "obj", ")", ":", "# The FP is captured for GC reasons inside the dtor closure", "# pylint: disable=invalid-name", "fd", "=", "open", "(", "obj", ")", "adapter", "=", "cmph", ".", "cmph_io_nlfile_adapter", "(", "fd", ")", "def", "dtor", "(", ")", ":", "cmph", ".", "cmph_io_nlfile_adapter_destroy", "(", "adapter", ")", "fd", ".", "close", "(", ")", "# pylint: enable=invalid-name", "return", "_AdapterCxt", "(", "adapter", ",", "dtor", ")", "elif", "is_file", "(", "obj", ")", ":", "adapter", "=", "cmph", ".", "cmph_io_nlfile_adapter", "(", "obj", ")", "dtor", "=", "lambda", ":", "cmph", ".", "cmph_io_nlfile_adapter_destroy", "(", "adapter", ")", "return", "_AdapterCxt", "(", "adapter", ",", "dtor", ")", "elif", "isinstance", "(", "obj", ",", "Sequence", ")", ":", "if", "len", "(", "obj", ")", "==", "0", ":", "raise", "ValueError", "(", "\"An empty sequence is already a perfect hash!\"", ")", "return", "_create_pyobj_adapter", "(", "cmph", ",", "ffi", ",", "obj", ")", "else", ":", "raise", "ValueError", "(", "\"data cannot have a cmph wrapper generated\"", ")" ]
85298572e51675cd0c7ef1052ed9989b0e57f0cc
test
YearlyFinancials.nature
Sets the nature of this YearlyFinancials. Nature of the balancesheet :param nature: The nature of this YearlyFinancials. :type: str
probe/models/yearly_financials.py
def nature(self, nature): """ Sets the nature of this YearlyFinancials. Nature of the balancesheet :param nature: The nature of this YearlyFinancials. :type: str """ allowed_values = ["STANDALONE"] if nature not in allowed_values: raise ValueError( "Invalid value for `nature`, must be one of {0}" .format(allowed_values) ) self._nature = nature
def nature(self, nature): """ Sets the nature of this YearlyFinancials. Nature of the balancesheet :param nature: The nature of this YearlyFinancials. :type: str """ allowed_values = ["STANDALONE"] if nature not in allowed_values: raise ValueError( "Invalid value for `nature`, must be one of {0}" .format(allowed_values) ) self._nature = nature
[ "Sets", "the", "nature", "of", "this", "YearlyFinancials", ".", "Nature", "of", "the", "balancesheet" ]
loanzen/probe-py
python
https://github.com/loanzen/probe-py/blob/b5dbb0dba26c9b451e9bf1dec9e1aaa7f42d75a5/probe/models/yearly_financials.py#L176-L190
[ "def", "nature", "(", "self", ",", "nature", ")", ":", "allowed_values", "=", "[", "\"STANDALONE\"", "]", "if", "nature", "not", "in", "allowed_values", ":", "raise", "ValueError", "(", "\"Invalid value for `nature`, must be one of {0}\"", ".", "format", "(", "allowed_values", ")", ")", "self", ".", "_nature", "=", "nature" ]
b5dbb0dba26c9b451e9bf1dec9e1aaa7f42d75a5
test
computed_displaywidth
Figure out a reasonable default with. Use os.environ['COLUMNS'] if possible, and failing that use 80.
columnize.py
def computed_displaywidth(): '''Figure out a reasonable default with. Use os.environ['COLUMNS'] if possible, and failing that use 80. ''' try: width = int(os.environ['COLUMNS']) except (KeyError, ValueError): width = get_terminal_size().columns return width or 80
def computed_displaywidth(): '''Figure out a reasonable default with. Use os.environ['COLUMNS'] if possible, and failing that use 80. ''' try: width = int(os.environ['COLUMNS']) except (KeyError, ValueError): width = get_terminal_size().columns return width or 80
[ "Figure", "out", "a", "reasonable", "default", "with", ".", "Use", "os", ".", "environ", "[", "COLUMNS", "]", "if", "possible", "and", "failing", "that", "use", "80", "." ]
rocky/pycolumnize
python
https://github.com/rocky/pycolumnize/blob/4373faa989884d3a25f276f9bbb4911cc71eca17/columnize.py#L14-L23
[ "def", "computed_displaywidth", "(", ")", ":", "try", ":", "width", "=", "int", "(", "os", ".", "environ", "[", "'COLUMNS'", "]", ")", "except", "(", "KeyError", ",", "ValueError", ")", ":", "width", "=", "get_terminal_size", "(", ")", ".", "columns", "return", "width", "or", "80" ]
4373faa989884d3a25f276f9bbb4911cc71eca17
test
columnize
Return a list of strings as a compact set of columns arranged horizontally or vertically. For example, for a line width of 4 characters (arranged vertically): ['1', '2,', '3', '4'] => '1 3\n2 4\n' or arranged horizontally: ['1', '2,', '3', '4'] => '1 2\n3 4\n' Each column is only as wide as necessary. By default, columns are separated by two spaces - one was not legible enough. Set "colsep" to adjust the string separate columns. Set `displaywidth' to set the line width. Normally, consecutive items go down from the top to bottom from the left-most column to the right-most. If "arrange_vertical" is set false, consecutive items will go across, left to right, top to bottom.
columnize.py
def columnize(array, displaywidth=80, colsep = ' ', arrange_vertical=True, ljust=True, lineprefix='', opts={}): """Return a list of strings as a compact set of columns arranged horizontally or vertically. For example, for a line width of 4 characters (arranged vertically): ['1', '2,', '3', '4'] => '1 3\n2 4\n' or arranged horizontally: ['1', '2,', '3', '4'] => '1 2\n3 4\n' Each column is only as wide as necessary. By default, columns are separated by two spaces - one was not legible enough. Set "colsep" to adjust the string separate columns. Set `displaywidth' to set the line width. Normally, consecutive items go down from the top to bottom from the left-most column to the right-most. If "arrange_vertical" is set false, consecutive items will go across, left to right, top to bottom.""" if not isinstance(array, (list, tuple)): raise TypeError(( 'array needs to be an instance of a list or a tuple')) o = {} if len(opts.keys()) > 0: for key in default_opts.keys(): o[key] = get_option(key, opts) pass if o['arrange_array']: o['array_prefix'] = '[' o['lineprefix'] = ' ' o['linesuffix'] = ",\n" o['array_suffix'] = "]\n" o['colsep'] = ', ' o['arrange_vertical'] = False pass else: o = default_opts.copy() o['displaywidth'] = displaywidth o['colsep'] = colsep o['arrange_vertical'] = arrange_vertical o['ljust'] = ljust o['lineprefix'] = lineprefix pass # if o['ljust'] is None: # o['ljust'] = !(list.all?{|datum| datum.kind_of?(Numeric)}) # pass if o['colfmt']: array = [(o['colfmt'] % i) for i in array] else: array = [str(i) for i in array] pass # Some degenerate cases size = len(array) if 0 == size: return "<empty>\n" elif size == 1: return '%s%s%s\n' % (o['array_prefix'], str(array[0]), o['array_suffix']) o['displaywidth'] = max(4, o['displaywidth'] - len(o['lineprefix'])) if o['arrange_vertical']: array_index = lambda nrows, row, col: nrows*col + row # Try every row count from 1 upwards for nrows in range(1, size+1): ncols = (size+nrows-1) // nrows colwidths = [] totwidth = -len(o['colsep']) for col in range(ncols): # get max column width for this column colwidth = 0 for row in range(nrows): i = array_index(nrows, row, col) if i >= size: break x = array[i] colwidth = max(colwidth, len(x)) pass colwidths.append(colwidth) totwidth += colwidth + len(o['colsep']) if totwidth > o['displaywidth']: break pass if totwidth <= o['displaywidth']: break pass # The smallest number of rows computed and the # max widths for each column has been obtained. # Now we just have to format each of the # rows. s = '' for row in range(nrows): texts = [] for col in range(ncols): i = row + nrows*col if i >= size: x = "" else: x = array[i] texts.append(x) while texts and not texts[-1]: del texts[-1] for col in range(len(texts)): if o['ljust']: texts[col] = texts[col].ljust(colwidths[col]) else: texts[col] = texts[col].rjust(colwidths[col]) pass pass s += "%s%s%s" % (o['lineprefix'], str(o['colsep'].join(texts)), o['linesuffix']) pass return s else: array_index = lambda ncols, row, col: ncols*(row-1) + col # Try every column count from size downwards colwidths = [] for ncols in range(size, 0, -1): # Try every row count from 1 upwards min_rows = (size+ncols-1) // ncols nrows = min_rows -1 while nrows < size: nrows += 1 rounded_size = nrows * ncols colwidths = [] totwidth = -len(o['colsep']) for col in range(ncols): # get max column width for this column colwidth = 0 for row in range(1, nrows+1): i = array_index(ncols, row, col) if i >= rounded_size: break elif i < size: x = array[i] colwidth = max(colwidth, len(x)) pass pass colwidths.append(colwidth) totwidth += colwidth + len(o['colsep']) if totwidth >= o['displaywidth']: break pass if totwidth <= o['displaywidth'] and i >= rounded_size-1: # Found the right nrows and ncols # print "right nrows and ncols" nrows = row break elif totwidth >= o['displaywidth']: # print "reduce ncols", ncols # Need to reduce ncols break pass if totwidth <= o['displaywidth'] and i >= rounded_size-1: break pass # The smallest number of rows computed and the # max widths for each column has been obtained. # Now we just have to format each of the # rows. s = '' if len(o['array_prefix']) != 0: prefix = o['array_prefix'] else: prefix = o['lineprefix'] pass for row in range(1, nrows+1): texts = [] for col in range(ncols): i = array_index(ncols, row, col) if i >= size: break else: x = array[i] texts.append(x) pass for col in range(len(texts)): if o['ljust']: texts[col] = texts[col].ljust(colwidths[col]) else: texts[col] = texts[col].rjust(colwidths[col]) pass pass s += "%s%s%s" % (prefix, str(o['colsep'].join(texts)), o['linesuffix']) prefix = o['lineprefix'] pass if o['arrange_array']: colsep = o['colsep'].rstrip() colsep_pos = -(len(colsep)+1) if s[colsep_pos:] == colsep + "\n": s = s[:colsep_pos] + o['array_suffix'] + "\n" pass pass else: s += o['array_suffix'] pass return s pass
def columnize(array, displaywidth=80, colsep = ' ', arrange_vertical=True, ljust=True, lineprefix='', opts={}): """Return a list of strings as a compact set of columns arranged horizontally or vertically. For example, for a line width of 4 characters (arranged vertically): ['1', '2,', '3', '4'] => '1 3\n2 4\n' or arranged horizontally: ['1', '2,', '3', '4'] => '1 2\n3 4\n' Each column is only as wide as necessary. By default, columns are separated by two spaces - one was not legible enough. Set "colsep" to adjust the string separate columns. Set `displaywidth' to set the line width. Normally, consecutive items go down from the top to bottom from the left-most column to the right-most. If "arrange_vertical" is set false, consecutive items will go across, left to right, top to bottom.""" if not isinstance(array, (list, tuple)): raise TypeError(( 'array needs to be an instance of a list or a tuple')) o = {} if len(opts.keys()) > 0: for key in default_opts.keys(): o[key] = get_option(key, opts) pass if o['arrange_array']: o['array_prefix'] = '[' o['lineprefix'] = ' ' o['linesuffix'] = ",\n" o['array_suffix'] = "]\n" o['colsep'] = ', ' o['arrange_vertical'] = False pass else: o = default_opts.copy() o['displaywidth'] = displaywidth o['colsep'] = colsep o['arrange_vertical'] = arrange_vertical o['ljust'] = ljust o['lineprefix'] = lineprefix pass # if o['ljust'] is None: # o['ljust'] = !(list.all?{|datum| datum.kind_of?(Numeric)}) # pass if o['colfmt']: array = [(o['colfmt'] % i) for i in array] else: array = [str(i) for i in array] pass # Some degenerate cases size = len(array) if 0 == size: return "<empty>\n" elif size == 1: return '%s%s%s\n' % (o['array_prefix'], str(array[0]), o['array_suffix']) o['displaywidth'] = max(4, o['displaywidth'] - len(o['lineprefix'])) if o['arrange_vertical']: array_index = lambda nrows, row, col: nrows*col + row # Try every row count from 1 upwards for nrows in range(1, size+1): ncols = (size+nrows-1) // nrows colwidths = [] totwidth = -len(o['colsep']) for col in range(ncols): # get max column width for this column colwidth = 0 for row in range(nrows): i = array_index(nrows, row, col) if i >= size: break x = array[i] colwidth = max(colwidth, len(x)) pass colwidths.append(colwidth) totwidth += colwidth + len(o['colsep']) if totwidth > o['displaywidth']: break pass if totwidth <= o['displaywidth']: break pass # The smallest number of rows computed and the # max widths for each column has been obtained. # Now we just have to format each of the # rows. s = '' for row in range(nrows): texts = [] for col in range(ncols): i = row + nrows*col if i >= size: x = "" else: x = array[i] texts.append(x) while texts and not texts[-1]: del texts[-1] for col in range(len(texts)): if o['ljust']: texts[col] = texts[col].ljust(colwidths[col]) else: texts[col] = texts[col].rjust(colwidths[col]) pass pass s += "%s%s%s" % (o['lineprefix'], str(o['colsep'].join(texts)), o['linesuffix']) pass return s else: array_index = lambda ncols, row, col: ncols*(row-1) + col # Try every column count from size downwards colwidths = [] for ncols in range(size, 0, -1): # Try every row count from 1 upwards min_rows = (size+ncols-1) // ncols nrows = min_rows -1 while nrows < size: nrows += 1 rounded_size = nrows * ncols colwidths = [] totwidth = -len(o['colsep']) for col in range(ncols): # get max column width for this column colwidth = 0 for row in range(1, nrows+1): i = array_index(ncols, row, col) if i >= rounded_size: break elif i < size: x = array[i] colwidth = max(colwidth, len(x)) pass pass colwidths.append(colwidth) totwidth += colwidth + len(o['colsep']) if totwidth >= o['displaywidth']: break pass if totwidth <= o['displaywidth'] and i >= rounded_size-1: # Found the right nrows and ncols # print "right nrows and ncols" nrows = row break elif totwidth >= o['displaywidth']: # print "reduce ncols", ncols # Need to reduce ncols break pass if totwidth <= o['displaywidth'] and i >= rounded_size-1: break pass # The smallest number of rows computed and the # max widths for each column has been obtained. # Now we just have to format each of the # rows. s = '' if len(o['array_prefix']) != 0: prefix = o['array_prefix'] else: prefix = o['lineprefix'] pass for row in range(1, nrows+1): texts = [] for col in range(ncols): i = array_index(ncols, row, col) if i >= size: break else: x = array[i] texts.append(x) pass for col in range(len(texts)): if o['ljust']: texts[col] = texts[col].ljust(colwidths[col]) else: texts[col] = texts[col].rjust(colwidths[col]) pass pass s += "%s%s%s" % (prefix, str(o['colsep'].join(texts)), o['linesuffix']) prefix = o['lineprefix'] pass if o['arrange_array']: colsep = o['colsep'].rstrip() colsep_pos = -(len(colsep)+1) if s[colsep_pos:] == colsep + "\n": s = s[:colsep_pos] + o['array_suffix'] + "\n" pass pass else: s += o['array_suffix'] pass return s pass
[ "Return", "a", "list", "of", "strings", "as", "a", "compact", "set", "of", "columns", "arranged", "horizontally", "or", "vertically", "." ]
rocky/pycolumnize
python
https://github.com/rocky/pycolumnize/blob/4373faa989884d3a25f276f9bbb4911cc71eca17/columnize.py#L45-L246
[ "def", "columnize", "(", "array", ",", "displaywidth", "=", "80", ",", "colsep", "=", "' '", ",", "arrange_vertical", "=", "True", ",", "ljust", "=", "True", ",", "lineprefix", "=", "''", ",", "opts", "=", "{", "}", ")", ":", "if", "not", "isinstance", "(", "array", ",", "(", "list", ",", "tuple", ")", ")", ":", "raise", "TypeError", "(", "(", "'array needs to be an instance of a list or a tuple'", ")", ")", "o", "=", "{", "}", "if", "len", "(", "opts", ".", "keys", "(", ")", ")", ">", "0", ":", "for", "key", "in", "default_opts", ".", "keys", "(", ")", ":", "o", "[", "key", "]", "=", "get_option", "(", "key", ",", "opts", ")", "pass", "if", "o", "[", "'arrange_array'", "]", ":", "o", "[", "'array_prefix'", "]", "=", "'['", "o", "[", "'lineprefix'", "]", "=", "' '", "o", "[", "'linesuffix'", "]", "=", "\",\\n\"", "o", "[", "'array_suffix'", "]", "=", "\"]\\n\"", "o", "[", "'colsep'", "]", "=", "', '", "o", "[", "'arrange_vertical'", "]", "=", "False", "pass", "else", ":", "o", "=", "default_opts", ".", "copy", "(", ")", "o", "[", "'displaywidth'", "]", "=", "displaywidth", "o", "[", "'colsep'", "]", "=", "colsep", "o", "[", "'arrange_vertical'", "]", "=", "arrange_vertical", "o", "[", "'ljust'", "]", "=", "ljust", "o", "[", "'lineprefix'", "]", "=", "lineprefix", "pass", "# if o['ljust'] is None:", "# o['ljust'] = !(list.all?{|datum| datum.kind_of?(Numeric)})", "# pass", "if", "o", "[", "'colfmt'", "]", ":", "array", "=", "[", "(", "o", "[", "'colfmt'", "]", "%", "i", ")", "for", "i", "in", "array", "]", "else", ":", "array", "=", "[", "str", "(", "i", ")", "for", "i", "in", "array", "]", "pass", "# Some degenerate cases", "size", "=", "len", "(", "array", ")", "if", "0", "==", "size", ":", "return", "\"<empty>\\n\"", "elif", "size", "==", "1", ":", "return", "'%s%s%s\\n'", "%", "(", "o", "[", "'array_prefix'", "]", ",", "str", "(", "array", "[", "0", "]", ")", ",", "o", "[", "'array_suffix'", "]", ")", "o", "[", "'displaywidth'", "]", "=", "max", "(", "4", ",", "o", "[", "'displaywidth'", "]", "-", "len", "(", "o", "[", "'lineprefix'", "]", ")", ")", "if", "o", "[", "'arrange_vertical'", "]", ":", "array_index", "=", "lambda", "nrows", ",", "row", ",", "col", ":", "nrows", "*", "col", "+", "row", "# Try every row count from 1 upwards", "for", "nrows", "in", "range", "(", "1", ",", "size", "+", "1", ")", ":", "ncols", "=", "(", "size", "+", "nrows", "-", "1", ")", "//", "nrows", "colwidths", "=", "[", "]", "totwidth", "=", "-", "len", "(", "o", "[", "'colsep'", "]", ")", "for", "col", "in", "range", "(", "ncols", ")", ":", "# get max column width for this column", "colwidth", "=", "0", "for", "row", "in", "range", "(", "nrows", ")", ":", "i", "=", "array_index", "(", "nrows", ",", "row", ",", "col", ")", "if", "i", ">=", "size", ":", "break", "x", "=", "array", "[", "i", "]", "colwidth", "=", "max", "(", "colwidth", ",", "len", "(", "x", ")", ")", "pass", "colwidths", ".", "append", "(", "colwidth", ")", "totwidth", "+=", "colwidth", "+", "len", "(", "o", "[", "'colsep'", "]", ")", "if", "totwidth", ">", "o", "[", "'displaywidth'", "]", ":", "break", "pass", "if", "totwidth", "<=", "o", "[", "'displaywidth'", "]", ":", "break", "pass", "# The smallest number of rows computed and the", "# max widths for each column has been obtained.", "# Now we just have to format each of the", "# rows.", "s", "=", "''", "for", "row", "in", "range", "(", "nrows", ")", ":", "texts", "=", "[", "]", "for", "col", "in", "range", "(", "ncols", ")", ":", "i", "=", "row", "+", "nrows", "*", "col", "if", "i", ">=", "size", ":", "x", "=", "\"\"", "else", ":", "x", "=", "array", "[", "i", "]", "texts", ".", "append", "(", "x", ")", "while", "texts", "and", "not", "texts", "[", "-", "1", "]", ":", "del", "texts", "[", "-", "1", "]", "for", "col", "in", "range", "(", "len", "(", "texts", ")", ")", ":", "if", "o", "[", "'ljust'", "]", ":", "texts", "[", "col", "]", "=", "texts", "[", "col", "]", ".", "ljust", "(", "colwidths", "[", "col", "]", ")", "else", ":", "texts", "[", "col", "]", "=", "texts", "[", "col", "]", ".", "rjust", "(", "colwidths", "[", "col", "]", ")", "pass", "pass", "s", "+=", "\"%s%s%s\"", "%", "(", "o", "[", "'lineprefix'", "]", ",", "str", "(", "o", "[", "'colsep'", "]", ".", "join", "(", "texts", ")", ")", ",", "o", "[", "'linesuffix'", "]", ")", "pass", "return", "s", "else", ":", "array_index", "=", "lambda", "ncols", ",", "row", ",", "col", ":", "ncols", "*", "(", "row", "-", "1", ")", "+", "col", "# Try every column count from size downwards", "colwidths", "=", "[", "]", "for", "ncols", "in", "range", "(", "size", ",", "0", ",", "-", "1", ")", ":", "# Try every row count from 1 upwards", "min_rows", "=", "(", "size", "+", "ncols", "-", "1", ")", "//", "ncols", "nrows", "=", "min_rows", "-", "1", "while", "nrows", "<", "size", ":", "nrows", "+=", "1", "rounded_size", "=", "nrows", "*", "ncols", "colwidths", "=", "[", "]", "totwidth", "=", "-", "len", "(", "o", "[", "'colsep'", "]", ")", "for", "col", "in", "range", "(", "ncols", ")", ":", "# get max column width for this column", "colwidth", "=", "0", "for", "row", "in", "range", "(", "1", ",", "nrows", "+", "1", ")", ":", "i", "=", "array_index", "(", "ncols", ",", "row", ",", "col", ")", "if", "i", ">=", "rounded_size", ":", "break", "elif", "i", "<", "size", ":", "x", "=", "array", "[", "i", "]", "colwidth", "=", "max", "(", "colwidth", ",", "len", "(", "x", ")", ")", "pass", "pass", "colwidths", ".", "append", "(", "colwidth", ")", "totwidth", "+=", "colwidth", "+", "len", "(", "o", "[", "'colsep'", "]", ")", "if", "totwidth", ">=", "o", "[", "'displaywidth'", "]", ":", "break", "pass", "if", "totwidth", "<=", "o", "[", "'displaywidth'", "]", "and", "i", ">=", "rounded_size", "-", "1", ":", "# Found the right nrows and ncols", "# print \"right nrows and ncols\"", "nrows", "=", "row", "break", "elif", "totwidth", ">=", "o", "[", "'displaywidth'", "]", ":", "# print \"reduce ncols\", ncols", "# Need to reduce ncols", "break", "pass", "if", "totwidth", "<=", "o", "[", "'displaywidth'", "]", "and", "i", ">=", "rounded_size", "-", "1", ":", "break", "pass", "# The smallest number of rows computed and the", "# max widths for each column has been obtained.", "# Now we just have to format each of the", "# rows.", "s", "=", "''", "if", "len", "(", "o", "[", "'array_prefix'", "]", ")", "!=", "0", ":", "prefix", "=", "o", "[", "'array_prefix'", "]", "else", ":", "prefix", "=", "o", "[", "'lineprefix'", "]", "pass", "for", "row", "in", "range", "(", "1", ",", "nrows", "+", "1", ")", ":", "texts", "=", "[", "]", "for", "col", "in", "range", "(", "ncols", ")", ":", "i", "=", "array_index", "(", "ncols", ",", "row", ",", "col", ")", "if", "i", ">=", "size", ":", "break", "else", ":", "x", "=", "array", "[", "i", "]", "texts", ".", "append", "(", "x", ")", "pass", "for", "col", "in", "range", "(", "len", "(", "texts", ")", ")", ":", "if", "o", "[", "'ljust'", "]", ":", "texts", "[", "col", "]", "=", "texts", "[", "col", "]", ".", "ljust", "(", "colwidths", "[", "col", "]", ")", "else", ":", "texts", "[", "col", "]", "=", "texts", "[", "col", "]", ".", "rjust", "(", "colwidths", "[", "col", "]", ")", "pass", "pass", "s", "+=", "\"%s%s%s\"", "%", "(", "prefix", ",", "str", "(", "o", "[", "'colsep'", "]", ".", "join", "(", "texts", ")", ")", ",", "o", "[", "'linesuffix'", "]", ")", "prefix", "=", "o", "[", "'lineprefix'", "]", "pass", "if", "o", "[", "'arrange_array'", "]", ":", "colsep", "=", "o", "[", "'colsep'", "]", ".", "rstrip", "(", ")", "colsep_pos", "=", "-", "(", "len", "(", "colsep", ")", "+", "1", ")", "if", "s", "[", "colsep_pos", ":", "]", "==", "colsep", "+", "\"\\n\"", ":", "s", "=", "s", "[", ":", "colsep_pos", "]", "+", "o", "[", "'array_suffix'", "]", "+", "\"\\n\"", "pass", "pass", "else", ":", "s", "+=", "o", "[", "'array_suffix'", "]", "pass", "return", "s", "pass" ]
4373faa989884d3a25f276f9bbb4911cc71eca17
test
generate_hash
Generates a new Minimal Perfect Hash (MPH) Parameters ---------- data : list, array-like, file-like The input that is used to generate the minimal perfect hash. Be aware, in most cases the input is expected to be distinct, and many of the algorithms benefit from the input being sorted. algorithm : string, optional {chd_ph (default), chd, bmz, bmz8, chm, brz, fch, bdz, bdz_ph} The algorithm to use in generating MPH's, choice of: chd / chd_ph - Compress Hash and Displace (default) (http://cmph.sourceforge.net/chd.html) - It is the fastest algorithm to build PHFs and MPHFs in linear time. - It generates the most compact PHFs and MPHFs we know of. - It can generate PHFs with a load factor up to 99 %. - It can be used to generate t-perfect hash functions. A t-perfect hash function allows at most t collisions in a given bin. It is a well-known fact that modern memories are organized as blocks which constitute transfer unit. Example of such blocks are cache lines for internal memory or sectors for hard disks. Thus, it can be very useful for devices that carry out I/O operations in blocks. - It is a two level scheme. It uses a first level hash function to split the key set in buckets of average size determined by a parameter b in the range [1,32]. In the second level it uses displacement values to resolve the collisions that have given rise to the buckets. - It can generate MPHFs that can be stored in approximately 2.07 bits per key. - For a load factor equal to the maximum one that is achieved by the BDZ algorithm (81 %), the resulting PHFs are stored in approximately 1.40 bits per key. bdz - BDZ / BPZ algorithm (http://cmph.sourceforge.net/bdz.html) - It is very simple and efficient. It outperforms all others except CHD. - It constructs both PHFs and MPHFs in linear time. - The maximum load factor one can achieve for a PHF is 1/1.23. - It is based on acyclic random 3-graphs. A 3-graph is a generalization of a graph where each edge connects 3 vertices instead of only 2. - The resulting MPHFs are not order preserving. - The resulting MPHFs can be stored in only (2 + x)cn bits, where c should be larger than or equal to 1.23 and x is a constant larger than 0 (actually, x = 1/b and b is a parameter that should be larger than 2). For c = 1.23 and b = 8, the resulting functions are stored in approximately 2.6 bits per key. - For its maximum load factor (81 %), the resulting PHFs are stored in approximately 1.95 bits per key. bmz - Botelho, Menoti and Ziviani algorithm: (http://cmph.sourceforge.net/bdz.html) - Constructs MPHFs in linear time. - It is based on cyclic random graphs. This makes it faster than the CHM algorithm. - The resulting MPHFs are not order preserving. - The resulting MPHFs are more compact than the ones generated by the CHM algorithm and can be stored in 4cn bytes, where c is in the range [0.93,1.15]. brz - BRZ algorithm: (http://cmph.sourceforge.net/brz.html) - A very fast external memory based algorithm for constructing minimal perfect hash functions for sets in the order of billions of keys. - It works in linear time. - The resulting MPHFs are not order preserving. - The resulting MPHFs can be stored using less than 8.0 bits per key. chm - Czech, Havas and Majewski algorithm: (http://cmph.sourceforge.net/chm.html) - Construct minimal MPHFs in linear time. - It is based on acyclic random graphs - The resulting MPHFs are order preserving. - The resulting MPHFs are stored in 4cn bytes, where c is greater than 2. fch - Fox, Chen and Heath algorithm: (http://cmph.sourceforge.net/chm.html) - Construct minimal perfect hash functions that require less than 4 bits per key to be stored. - The resulting MPHFs are very compact and very efficient at evaluation time - The algorithm is only efficient for small sets. - It is used as internal algorithm in the BRZ algorithm to efficiently solve larger problems and even so to generate MPHFs that require approximately 4.1 bits per key to be stored. For that, you just need to set the parameters -a to brz and -c to a value larger than or equal to 2.6. hash_fns : list {jenkins (default), count} optional Internal hash functions to use inside MPH generation functions, can be multiple fns as a list. chd_keys_per_bin : int [1 to 128], optional Set the number of keys per bin for a t-perfect hashing function. A t-perfect hash function allows at most t collisions in a given bin. This parameter applies only to the `chd` and `chd_ph` algorithms. Its value should be an integer in the range [1, 128]. Default is 1 chd_load_factor : float, optional The load factor used in the `chd_ph` algorithm fch_bits_per_key : int, optional The number of bits per key required in the FCH algorithm num_graph_vertices : int, optional The number of vertices in the graph for the algorithms BMZ and CHM brz_memory_size : int (default 8), optional Main memory availability (in MB) used in BRZ algorithm Default is 8Mb brz_temp_dir : string, optional Temporary directory used in BRZ algorithm brz_max_keys_per_bucket : int [64 to 175] (default 128), optional Used to make the maximal number of keys in a bucket lower than 256. In this case its value should be an integer in the range [64,175]. Default is 128. bdz_precomputed_rank : int [3 to 10] (default 7), optional For BDZ it is used to determine the size of some precomputed rank information and its value should be an integer in the range [3,10]. Default is 7. The larger is this value, the more compact are the resulting functions and the slower are them at evaluation time. chd_avg_keys_per_bucket : int [1 to 32] (default 4), optional For CHD and CHD_PH it is used to set the average number of keys per bucket and its value should be an integer in the range [1,32]. Default is 4. The larger is this value, the slower is the construction of the functions. Returns ------- MPH A wrapper object that represents a minimal perfect hash in memory Raises ------ ValueError If arguments presented are incomplete, or incompatable RuntimeError If the MPH generation fails
cmph/__init__.py
def generate_hash(data, algorithm='chd_ph', hash_fns=(), chd_keys_per_bin=1, chd_load_factor=None, fch_bits_per_key=None, num_graph_vertices=None, brz_memory_size=8, brz_temp_dir=None, brz_max_keys_per_bucket=128, bdz_precomputed_rank=7, chd_avg_keys_per_bucket=4): """ Generates a new Minimal Perfect Hash (MPH) Parameters ---------- data : list, array-like, file-like The input that is used to generate the minimal perfect hash. Be aware, in most cases the input is expected to be distinct, and many of the algorithms benefit from the input being sorted. algorithm : string, optional {chd_ph (default), chd, bmz, bmz8, chm, brz, fch, bdz, bdz_ph} The algorithm to use in generating MPH's, choice of: chd / chd_ph - Compress Hash and Displace (default) (http://cmph.sourceforge.net/chd.html) - It is the fastest algorithm to build PHFs and MPHFs in linear time. - It generates the most compact PHFs and MPHFs we know of. - It can generate PHFs with a load factor up to 99 %. - It can be used to generate t-perfect hash functions. A t-perfect hash function allows at most t collisions in a given bin. It is a well-known fact that modern memories are organized as blocks which constitute transfer unit. Example of such blocks are cache lines for internal memory or sectors for hard disks. Thus, it can be very useful for devices that carry out I/O operations in blocks. - It is a two level scheme. It uses a first level hash function to split the key set in buckets of average size determined by a parameter b in the range [1,32]. In the second level it uses displacement values to resolve the collisions that have given rise to the buckets. - It can generate MPHFs that can be stored in approximately 2.07 bits per key. - For a load factor equal to the maximum one that is achieved by the BDZ algorithm (81 %), the resulting PHFs are stored in approximately 1.40 bits per key. bdz - BDZ / BPZ algorithm (http://cmph.sourceforge.net/bdz.html) - It is very simple and efficient. It outperforms all others except CHD. - It constructs both PHFs and MPHFs in linear time. - The maximum load factor one can achieve for a PHF is 1/1.23. - It is based on acyclic random 3-graphs. A 3-graph is a generalization of a graph where each edge connects 3 vertices instead of only 2. - The resulting MPHFs are not order preserving. - The resulting MPHFs can be stored in only (2 + x)cn bits, where c should be larger than or equal to 1.23 and x is a constant larger than 0 (actually, x = 1/b and b is a parameter that should be larger than 2). For c = 1.23 and b = 8, the resulting functions are stored in approximately 2.6 bits per key. - For its maximum load factor (81 %), the resulting PHFs are stored in approximately 1.95 bits per key. bmz - Botelho, Menoti and Ziviani algorithm: (http://cmph.sourceforge.net/bdz.html) - Constructs MPHFs in linear time. - It is based on cyclic random graphs. This makes it faster than the CHM algorithm. - The resulting MPHFs are not order preserving. - The resulting MPHFs are more compact than the ones generated by the CHM algorithm and can be stored in 4cn bytes, where c is in the range [0.93,1.15]. brz - BRZ algorithm: (http://cmph.sourceforge.net/brz.html) - A very fast external memory based algorithm for constructing minimal perfect hash functions for sets in the order of billions of keys. - It works in linear time. - The resulting MPHFs are not order preserving. - The resulting MPHFs can be stored using less than 8.0 bits per key. chm - Czech, Havas and Majewski algorithm: (http://cmph.sourceforge.net/chm.html) - Construct minimal MPHFs in linear time. - It is based on acyclic random graphs - The resulting MPHFs are order preserving. - The resulting MPHFs are stored in 4cn bytes, where c is greater than 2. fch - Fox, Chen and Heath algorithm: (http://cmph.sourceforge.net/chm.html) - Construct minimal perfect hash functions that require less than 4 bits per key to be stored. - The resulting MPHFs are very compact and very efficient at evaluation time - The algorithm is only efficient for small sets. - It is used as internal algorithm in the BRZ algorithm to efficiently solve larger problems and even so to generate MPHFs that require approximately 4.1 bits per key to be stored. For that, you just need to set the parameters -a to brz and -c to a value larger than or equal to 2.6. hash_fns : list {jenkins (default), count} optional Internal hash functions to use inside MPH generation functions, can be multiple fns as a list. chd_keys_per_bin : int [1 to 128], optional Set the number of keys per bin for a t-perfect hashing function. A t-perfect hash function allows at most t collisions in a given bin. This parameter applies only to the `chd` and `chd_ph` algorithms. Its value should be an integer in the range [1, 128]. Default is 1 chd_load_factor : float, optional The load factor used in the `chd_ph` algorithm fch_bits_per_key : int, optional The number of bits per key required in the FCH algorithm num_graph_vertices : int, optional The number of vertices in the graph for the algorithms BMZ and CHM brz_memory_size : int (default 8), optional Main memory availability (in MB) used in BRZ algorithm Default is 8Mb brz_temp_dir : string, optional Temporary directory used in BRZ algorithm brz_max_keys_per_bucket : int [64 to 175] (default 128), optional Used to make the maximal number of keys in a bucket lower than 256. In this case its value should be an integer in the range [64,175]. Default is 128. bdz_precomputed_rank : int [3 to 10] (default 7), optional For BDZ it is used to determine the size of some precomputed rank information and its value should be an integer in the range [3,10]. Default is 7. The larger is this value, the more compact are the resulting functions and the slower are them at evaluation time. chd_avg_keys_per_bucket : int [1 to 32] (default 4), optional For CHD and CHD_PH it is used to set the average number of keys per bucket and its value should be an integer in the range [1,32]. Default is 4. The larger is this value, the slower is the construction of the functions. Returns ------- MPH A wrapper object that represents a minimal perfect hash in memory Raises ------ ValueError If arguments presented are incomplete, or incompatable RuntimeError If the MPH generation fails """ cfg = _cfg(algorithm, hash_fns, chd_keys_per_bin, chd_load_factor, fch_bits_per_key, num_graph_vertices, brz_memory_size, brz_temp_dir, brz_max_keys_per_bucket, bdz_precomputed_rank, chd_avg_keys_per_bucket) with create_adapter(_cmph, ffi, data) as source: with _create_config(source, cfg) as config: _mph = _cmph.cmph_new(config) if not _mph: raise RuntimeError("MPH generation failed") return MPH(_mph)
def generate_hash(data, algorithm='chd_ph', hash_fns=(), chd_keys_per_bin=1, chd_load_factor=None, fch_bits_per_key=None, num_graph_vertices=None, brz_memory_size=8, brz_temp_dir=None, brz_max_keys_per_bucket=128, bdz_precomputed_rank=7, chd_avg_keys_per_bucket=4): """ Generates a new Minimal Perfect Hash (MPH) Parameters ---------- data : list, array-like, file-like The input that is used to generate the minimal perfect hash. Be aware, in most cases the input is expected to be distinct, and many of the algorithms benefit from the input being sorted. algorithm : string, optional {chd_ph (default), chd, bmz, bmz8, chm, brz, fch, bdz, bdz_ph} The algorithm to use in generating MPH's, choice of: chd / chd_ph - Compress Hash and Displace (default) (http://cmph.sourceforge.net/chd.html) - It is the fastest algorithm to build PHFs and MPHFs in linear time. - It generates the most compact PHFs and MPHFs we know of. - It can generate PHFs with a load factor up to 99 %. - It can be used to generate t-perfect hash functions. A t-perfect hash function allows at most t collisions in a given bin. It is a well-known fact that modern memories are organized as blocks which constitute transfer unit. Example of such blocks are cache lines for internal memory or sectors for hard disks. Thus, it can be very useful for devices that carry out I/O operations in blocks. - It is a two level scheme. It uses a first level hash function to split the key set in buckets of average size determined by a parameter b in the range [1,32]. In the second level it uses displacement values to resolve the collisions that have given rise to the buckets. - It can generate MPHFs that can be stored in approximately 2.07 bits per key. - For a load factor equal to the maximum one that is achieved by the BDZ algorithm (81 %), the resulting PHFs are stored in approximately 1.40 bits per key. bdz - BDZ / BPZ algorithm (http://cmph.sourceforge.net/bdz.html) - It is very simple and efficient. It outperforms all others except CHD. - It constructs both PHFs and MPHFs in linear time. - The maximum load factor one can achieve for a PHF is 1/1.23. - It is based on acyclic random 3-graphs. A 3-graph is a generalization of a graph where each edge connects 3 vertices instead of only 2. - The resulting MPHFs are not order preserving. - The resulting MPHFs can be stored in only (2 + x)cn bits, where c should be larger than or equal to 1.23 and x is a constant larger than 0 (actually, x = 1/b and b is a parameter that should be larger than 2). For c = 1.23 and b = 8, the resulting functions are stored in approximately 2.6 bits per key. - For its maximum load factor (81 %), the resulting PHFs are stored in approximately 1.95 bits per key. bmz - Botelho, Menoti and Ziviani algorithm: (http://cmph.sourceforge.net/bdz.html) - Constructs MPHFs in linear time. - It is based on cyclic random graphs. This makes it faster than the CHM algorithm. - The resulting MPHFs are not order preserving. - The resulting MPHFs are more compact than the ones generated by the CHM algorithm and can be stored in 4cn bytes, where c is in the range [0.93,1.15]. brz - BRZ algorithm: (http://cmph.sourceforge.net/brz.html) - A very fast external memory based algorithm for constructing minimal perfect hash functions for sets in the order of billions of keys. - It works in linear time. - The resulting MPHFs are not order preserving. - The resulting MPHFs can be stored using less than 8.0 bits per key. chm - Czech, Havas and Majewski algorithm: (http://cmph.sourceforge.net/chm.html) - Construct minimal MPHFs in linear time. - It is based on acyclic random graphs - The resulting MPHFs are order preserving. - The resulting MPHFs are stored in 4cn bytes, where c is greater than 2. fch - Fox, Chen and Heath algorithm: (http://cmph.sourceforge.net/chm.html) - Construct minimal perfect hash functions that require less than 4 bits per key to be stored. - The resulting MPHFs are very compact and very efficient at evaluation time - The algorithm is only efficient for small sets. - It is used as internal algorithm in the BRZ algorithm to efficiently solve larger problems and even so to generate MPHFs that require approximately 4.1 bits per key to be stored. For that, you just need to set the parameters -a to brz and -c to a value larger than or equal to 2.6. hash_fns : list {jenkins (default), count} optional Internal hash functions to use inside MPH generation functions, can be multiple fns as a list. chd_keys_per_bin : int [1 to 128], optional Set the number of keys per bin for a t-perfect hashing function. A t-perfect hash function allows at most t collisions in a given bin. This parameter applies only to the `chd` and `chd_ph` algorithms. Its value should be an integer in the range [1, 128]. Default is 1 chd_load_factor : float, optional The load factor used in the `chd_ph` algorithm fch_bits_per_key : int, optional The number of bits per key required in the FCH algorithm num_graph_vertices : int, optional The number of vertices in the graph for the algorithms BMZ and CHM brz_memory_size : int (default 8), optional Main memory availability (in MB) used in BRZ algorithm Default is 8Mb brz_temp_dir : string, optional Temporary directory used in BRZ algorithm brz_max_keys_per_bucket : int [64 to 175] (default 128), optional Used to make the maximal number of keys in a bucket lower than 256. In this case its value should be an integer in the range [64,175]. Default is 128. bdz_precomputed_rank : int [3 to 10] (default 7), optional For BDZ it is used to determine the size of some precomputed rank information and its value should be an integer in the range [3,10]. Default is 7. The larger is this value, the more compact are the resulting functions and the slower are them at evaluation time. chd_avg_keys_per_bucket : int [1 to 32] (default 4), optional For CHD and CHD_PH it is used to set the average number of keys per bucket and its value should be an integer in the range [1,32]. Default is 4. The larger is this value, the slower is the construction of the functions. Returns ------- MPH A wrapper object that represents a minimal perfect hash in memory Raises ------ ValueError If arguments presented are incomplete, or incompatable RuntimeError If the MPH generation fails """ cfg = _cfg(algorithm, hash_fns, chd_keys_per_bin, chd_load_factor, fch_bits_per_key, num_graph_vertices, brz_memory_size, brz_temp_dir, brz_max_keys_per_bucket, bdz_precomputed_rank, chd_avg_keys_per_bucket) with create_adapter(_cmph, ffi, data) as source: with _create_config(source, cfg) as config: _mph = _cmph.cmph_new(config) if not _mph: raise RuntimeError("MPH generation failed") return MPH(_mph)
[ "Generates", "a", "new", "Minimal", "Perfect", "Hash", "(", "MPH", ")" ]
URXtech/cmph-cffi
python
https://github.com/URXtech/cmph-cffi/blob/85298572e51675cd0c7ef1052ed9989b0e57f0cc/cmph/__init__.py#L388-L567
[ "def", "generate_hash", "(", "data", ",", "algorithm", "=", "'chd_ph'", ",", "hash_fns", "=", "(", ")", ",", "chd_keys_per_bin", "=", "1", ",", "chd_load_factor", "=", "None", ",", "fch_bits_per_key", "=", "None", ",", "num_graph_vertices", "=", "None", ",", "brz_memory_size", "=", "8", ",", "brz_temp_dir", "=", "None", ",", "brz_max_keys_per_bucket", "=", "128", ",", "bdz_precomputed_rank", "=", "7", ",", "chd_avg_keys_per_bucket", "=", "4", ")", ":", "cfg", "=", "_cfg", "(", "algorithm", ",", "hash_fns", ",", "chd_keys_per_bin", ",", "chd_load_factor", ",", "fch_bits_per_key", ",", "num_graph_vertices", ",", "brz_memory_size", ",", "brz_temp_dir", ",", "brz_max_keys_per_bucket", ",", "bdz_precomputed_rank", ",", "chd_avg_keys_per_bucket", ")", "with", "create_adapter", "(", "_cmph", ",", "ffi", ",", "data", ")", "as", "source", ":", "with", "_create_config", "(", "source", ",", "cfg", ")", "as", "config", ":", "_mph", "=", "_cmph", ".", "cmph_new", "(", "config", ")", "if", "not", "_mph", ":", "raise", "RuntimeError", "(", "\"MPH generation failed\"", ")", "return", "MPH", "(", "_mph", ")" ]
85298572e51675cd0c7ef1052ed9989b0e57f0cc
test
load_hash
Load a Minimal Perfect Hash (MPH) Given an input stream, this will load a minimal perfect hash Parameters ---------- existing_mph : file_like, string An input stream that is file like, and able to load a preexisting MPH, or the filename representing it. Raises ------ IOError If there is an issue accessing or manipulating the underlying stream Returns ------- MPH A MPH wrapper class
cmph/__init__.py
def load_hash(existing_mph): """ Load a Minimal Perfect Hash (MPH) Given an input stream, this will load a minimal perfect hash Parameters ---------- existing_mph : file_like, string An input stream that is file like, and able to load a preexisting MPH, or the filename representing it. Raises ------ IOError If there is an issue accessing or manipulating the underlying stream Returns ------- MPH A MPH wrapper class """ if is_file_location(existing_mph): with open(abspath(existing_mph)) as hash_table: _mph = _cmph.cmph_load(hash_table) elif is_file(existing_mph): _mph = _cmph.cmph_load(existing_mph) if not _mph: raise IOError("Unable to load an MPH from the given source") return MPH(_mph)
def load_hash(existing_mph): """ Load a Minimal Perfect Hash (MPH) Given an input stream, this will load a minimal perfect hash Parameters ---------- existing_mph : file_like, string An input stream that is file like, and able to load a preexisting MPH, or the filename representing it. Raises ------ IOError If there is an issue accessing or manipulating the underlying stream Returns ------- MPH A MPH wrapper class """ if is_file_location(existing_mph): with open(abspath(existing_mph)) as hash_table: _mph = _cmph.cmph_load(hash_table) elif is_file(existing_mph): _mph = _cmph.cmph_load(existing_mph) if not _mph: raise IOError("Unable to load an MPH from the given source") return MPH(_mph)
[ "Load", "a", "Minimal", "Perfect", "Hash", "(", "MPH", ")", "Given", "an", "input", "stream", "this", "will", "load", "a", "minimal", "perfect", "hash" ]
URXtech/cmph-cffi
python
https://github.com/URXtech/cmph-cffi/blob/85298572e51675cd0c7ef1052ed9989b0e57f0cc/cmph/__init__.py#L570-L601
[ "def", "load_hash", "(", "existing_mph", ")", ":", "if", "is_file_location", "(", "existing_mph", ")", ":", "with", "open", "(", "abspath", "(", "existing_mph", ")", ")", "as", "hash_table", ":", "_mph", "=", "_cmph", ".", "cmph_load", "(", "hash_table", ")", "elif", "is_file", "(", "existing_mph", ")", ":", "_mph", "=", "_cmph", ".", "cmph_load", "(", "existing_mph", ")", "if", "not", "_mph", ":", "raise", "IOError", "(", "\"Unable to load an MPH from the given source\"", ")", "return", "MPH", "(", "_mph", ")" ]
85298572e51675cd0c7ef1052ed9989b0e57f0cc
test
MPH.save
Persist the Minimal Perfect Hash (MPH) to a stream Parameters ---------- output : file_like The stream to use to persist the MPH Raises ------ IOError If there is an issue accessing or manipulating the underlying stream
cmph/__init__.py
def save(self, output): """ Persist the Minimal Perfect Hash (MPH) to a stream Parameters ---------- output : file_like The stream to use to persist the MPH Raises ------ IOError If there is an issue accessing or manipulating the underlying stream """ assert self._mph, "There is no MPH ?" if isinstance(output, six.string_types): with open(abspath(output), 'w') as out: _cmph.cmph_dump(self._mph, out) else: _cmph.cmph_dump(self._mph, output)
def save(self, output): """ Persist the Minimal Perfect Hash (MPH) to a stream Parameters ---------- output : file_like The stream to use to persist the MPH Raises ------ IOError If there is an issue accessing or manipulating the underlying stream """ assert self._mph, "There is no MPH ?" if isinstance(output, six.string_types): with open(abspath(output), 'w') as out: _cmph.cmph_dump(self._mph, out) else: _cmph.cmph_dump(self._mph, output)
[ "Persist", "the", "Minimal", "Perfect", "Hash", "(", "MPH", ")", "to", "a", "stream" ]
URXtech/cmph-cffi
python
https://github.com/URXtech/cmph-cffi/blob/85298572e51675cd0c7ef1052ed9989b0e57f0cc/cmph/__init__.py#L230-L250
[ "def", "save", "(", "self", ",", "output", ")", ":", "assert", "self", ".", "_mph", ",", "\"There is no MPH ?\"", "if", "isinstance", "(", "output", ",", "six", ".", "string_types", ")", ":", "with", "open", "(", "abspath", "(", "output", ")", ",", "'w'", ")", "as", "out", ":", "_cmph", ".", "cmph_dump", "(", "self", ".", "_mph", ",", "out", ")", "else", ":", "_cmph", ".", "cmph_dump", "(", "self", ".", "_mph", ",", "output", ")" ]
85298572e51675cd0c7ef1052ed9989b0e57f0cc
test
MPH.lookup
Generate hash code for a key from the Minimal Perfect Hash (MPH) Parameters ---------- Key : object The item to generate a key for, this works best for keys that are strings, or can be transformed fairly directly into bytes Returns : int The code for the given item
cmph/__init__.py
def lookup(self, key): """ Generate hash code for a key from the Minimal Perfect Hash (MPH) Parameters ---------- Key : object The item to generate a key for, this works best for keys that are strings, or can be transformed fairly directly into bytes Returns : int The code for the given item """ assert self._mph key = convert_to_bytes(key) box = ffi.new('char[]', key) try: result = _cmph.cmph_search(self._mph, box, len(key)) return result finally: del box
def lookup(self, key): """ Generate hash code for a key from the Minimal Perfect Hash (MPH) Parameters ---------- Key : object The item to generate a key for, this works best for keys that are strings, or can be transformed fairly directly into bytes Returns : int The code for the given item """ assert self._mph key = convert_to_bytes(key) box = ffi.new('char[]', key) try: result = _cmph.cmph_search(self._mph, box, len(key)) return result finally: del box
[ "Generate", "hash", "code", "for", "a", "key", "from", "the", "Minimal", "Perfect", "Hash", "(", "MPH", ")" ]
URXtech/cmph-cffi
python
https://github.com/URXtech/cmph-cffi/blob/85298572e51675cd0c7ef1052ed9989b0e57f0cc/cmph/__init__.py#L252-L273
[ "def", "lookup", "(", "self", ",", "key", ")", ":", "assert", "self", ".", "_mph", "key", "=", "convert_to_bytes", "(", "key", ")", "box", "=", "ffi", ".", "new", "(", "'char[]'", ",", "key", ")", "try", ":", "result", "=", "_cmph", ".", "cmph_search", "(", "self", ".", "_mph", ",", "box", ",", "len", "(", "key", ")", ")", "return", "result", "finally", ":", "del", "box" ]
85298572e51675cd0c7ef1052ed9989b0e57f0cc
test
Section.update_
Update values of configuration section with dict. Args: sct_dict (dict): dict indexed with option names. Undefined options are discarded. conf_arg (bool): if True, only options that can be set in a config file are updated.
loam/manager.py
def update_(self, sct_dict, conf_arg=True): """Update values of configuration section with dict. Args: sct_dict (dict): dict indexed with option names. Undefined options are discarded. conf_arg (bool): if True, only options that can be set in a config file are updated. """ for opt, val in sct_dict.items(): if opt not in self.def_: continue if not conf_arg or self.def_[opt].conf_arg: self[opt] = val
def update_(self, sct_dict, conf_arg=True): """Update values of configuration section with dict. Args: sct_dict (dict): dict indexed with option names. Undefined options are discarded. conf_arg (bool): if True, only options that can be set in a config file are updated. """ for opt, val in sct_dict.items(): if opt not in self.def_: continue if not conf_arg or self.def_[opt].conf_arg: self[opt] = val
[ "Update", "values", "of", "configuration", "section", "with", "dict", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L123-L136
[ "def", "update_", "(", "self", ",", "sct_dict", ",", "conf_arg", "=", "True", ")", ":", "for", "opt", ",", "val", "in", "sct_dict", ".", "items", "(", ")", ":", "if", "opt", "not", "in", "self", ".", "def_", ":", "continue", "if", "not", "conf_arg", "or", "self", ".", "def_", "[", "opt", "]", ".", "conf_arg", ":", "self", "[", "opt", "]", "=", "val" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
Section.reset_
Restore default values of options in this section.
loam/manager.py
def reset_(self): """Restore default values of options in this section.""" for opt, meta in self.defaults_(): self[opt] = meta.default
def reset_(self): """Restore default values of options in this section.""" for opt, meta in self.defaults_(): self[opt] = meta.default
[ "Restore", "default", "values", "of", "options", "in", "this", "section", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L138-L141
[ "def", "reset_", "(", "self", ")", ":", "for", "opt", ",", "meta", "in", "self", ".", "defaults_", "(", ")", ":", "self", "[", "opt", "]", "=", "meta", ".", "default" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
ConfigurationManager.from_dict_
Use a dictionary to create a :class:`ConfigurationManager`. Args: conf_dict (dict of dict of :class:`ConfOpt`): the first level of keys should be the section names. The second level should be the option names. The values are the options metadata. Returns: :class:`ConfigurationManager`: a configuration manager with the requested sections and options.
loam/manager.py
def from_dict_(cls, conf_dict): """Use a dictionary to create a :class:`ConfigurationManager`. Args: conf_dict (dict of dict of :class:`ConfOpt`): the first level of keys should be the section names. The second level should be the option names. The values are the options metadata. Returns: :class:`ConfigurationManager`: a configuration manager with the requested sections and options. """ return cls(**{name: Section(**opts) for name, opts in conf_dict.items()})
def from_dict_(cls, conf_dict): """Use a dictionary to create a :class:`ConfigurationManager`. Args: conf_dict (dict of dict of :class:`ConfOpt`): the first level of keys should be the section names. The second level should be the option names. The values are the options metadata. Returns: :class:`ConfigurationManager`: a configuration manager with the requested sections and options. """ return cls(**{name: Section(**opts) for name, opts in conf_dict.items()})
[ "Use", "a", "dictionary", "to", "create", "a", ":", "class", ":", "ConfigurationManager", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L187-L200
[ "def", "from_dict_", "(", "cls", ",", "conf_dict", ")", ":", "return", "cls", "(", "*", "*", "{", "name", ":", "Section", "(", "*", "*", "opts", ")", "for", "name", ",", "opts", "in", "conf_dict", ".", "items", "(", ")", "}", ")" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
ConfigurationManager.set_config_files_
Set the list of config files. Args: config_files (pathlike): path of config files, given in the order of reading.
loam/manager.py
def set_config_files_(self, *config_files): """Set the list of config files. Args: config_files (pathlike): path of config files, given in the order of reading. """ self._config_files = tuple(pathlib.Path(path) for path in config_files)
def set_config_files_(self, *config_files): """Set the list of config files. Args: config_files (pathlike): path of config files, given in the order of reading. """ self._config_files = tuple(pathlib.Path(path) for path in config_files)
[ "Set", "the", "list", "of", "config", "files", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L212-L219
[ "def", "set_config_files_", "(", "self", ",", "*", "config_files", ")", ":", "self", ".", "_config_files", "=", "tuple", "(", "pathlib", ".", "Path", "(", "path", ")", "for", "path", "in", "config_files", ")" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
ConfigurationManager.opt_vals_
Iterator over sections, option names, and option values. This iterator is also implemented at the section level. The two loops produce the same output:: for sct, opt, val in conf.opt_vals_(): print(sct, opt, val) for sct in conf.sections_(): for opt, val in conf[sct].opt_vals_(): print(sct, opt, val) Yields: tuples with sections, option names, and option values.
loam/manager.py
def opt_vals_(self): """Iterator over sections, option names, and option values. This iterator is also implemented at the section level. The two loops produce the same output:: for sct, opt, val in conf.opt_vals_(): print(sct, opt, val) for sct in conf.sections_(): for opt, val in conf[sct].opt_vals_(): print(sct, opt, val) Yields: tuples with sections, option names, and option values. """ for sct, opt in self.options_(): yield sct, opt, self[sct][opt]
def opt_vals_(self): """Iterator over sections, option names, and option values. This iterator is also implemented at the section level. The two loops produce the same output:: for sct, opt, val in conf.opt_vals_(): print(sct, opt, val) for sct in conf.sections_(): for opt, val in conf[sct].opt_vals_(): print(sct, opt, val) Yields: tuples with sections, option names, and option values. """ for sct, opt in self.options_(): yield sct, opt, self[sct][opt]
[ "Iterator", "over", "sections", "option", "names", "and", "option", "values", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L267-L284
[ "def", "opt_vals_", "(", "self", ")", ":", "for", "sct", ",", "opt", "in", "self", ".", "options_", "(", ")", ":", "yield", "sct", ",", "opt", ",", "self", "[", "sct", "]", "[", "opt", "]" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
ConfigurationManager.defaults_
Iterator over sections, option names, and option metadata. This iterator is also implemented at the section level. The two loops produce the same output:: for sct, opt, meta in conf.defaults_(): print(sct, opt, meta.default) for sct in conf.sections_(): for opt, meta in conf[sct].defaults_(): print(sct, opt, meta.default) Yields: tuples with sections, option names, and :class:`Conf` instances holding option metadata.
loam/manager.py
def defaults_(self): """Iterator over sections, option names, and option metadata. This iterator is also implemented at the section level. The two loops produce the same output:: for sct, opt, meta in conf.defaults_(): print(sct, opt, meta.default) for sct in conf.sections_(): for opt, meta in conf[sct].defaults_(): print(sct, opt, meta.default) Yields: tuples with sections, option names, and :class:`Conf` instances holding option metadata. """ for sct, opt in self.options_(): yield sct, opt, self[sct].def_[opt]
def defaults_(self): """Iterator over sections, option names, and option metadata. This iterator is also implemented at the section level. The two loops produce the same output:: for sct, opt, meta in conf.defaults_(): print(sct, opt, meta.default) for sct in conf.sections_(): for opt, meta in conf[sct].defaults_(): print(sct, opt, meta.default) Yields: tuples with sections, option names, and :class:`Conf` instances holding option metadata. """ for sct, opt in self.options_(): yield sct, opt, self[sct].def_[opt]
[ "Iterator", "over", "sections", "option", "names", "and", "option", "metadata", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L286-L304
[ "def", "defaults_", "(", "self", ")", ":", "for", "sct", ",", "opt", "in", "self", ".", "options_", "(", ")", ":", "yield", "sct", ",", "opt", ",", "self", "[", "sct", "]", ".", "def_", "[", "opt", "]" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
ConfigurationManager.create_config_
Create config file. Create config file in :attr:`config_files_[index]`. Parameters: index(int): index of config file. update (bool): if set to True and :attr:`config_files_` already exists, its content is read and all the options it sets are kept in the produced config file.
loam/manager.py
def create_config_(self, index=0, update=False): """Create config file. Create config file in :attr:`config_files_[index]`. Parameters: index(int): index of config file. update (bool): if set to True and :attr:`config_files_` already exists, its content is read and all the options it sets are kept in the produced config file. """ if not self.config_files_[index:]: return path = self.config_files_[index] if not path.parent.exists(): path.parent.mkdir(parents=True) conf_dict = {} for section in self.sections_(): conf_opts = [o for o, m in self[section].defaults_() if m.conf_arg] if not conf_opts: continue conf_dict[section] = {} for opt in conf_opts: conf_dict[section][opt] = (self[section][opt] if update else self[section].def_[opt].default) with path.open('w') as cfile: toml.dump(conf_dict, cfile)
def create_config_(self, index=0, update=False): """Create config file. Create config file in :attr:`config_files_[index]`. Parameters: index(int): index of config file. update (bool): if set to True and :attr:`config_files_` already exists, its content is read and all the options it sets are kept in the produced config file. """ if not self.config_files_[index:]: return path = self.config_files_[index] if not path.parent.exists(): path.parent.mkdir(parents=True) conf_dict = {} for section in self.sections_(): conf_opts = [o for o, m in self[section].defaults_() if m.conf_arg] if not conf_opts: continue conf_dict[section] = {} for opt in conf_opts: conf_dict[section][opt] = (self[section][opt] if update else self[section].def_[opt].default) with path.open('w') as cfile: toml.dump(conf_dict, cfile)
[ "Create", "config", "file", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L311-L337
[ "def", "create_config_", "(", "self", ",", "index", "=", "0", ",", "update", "=", "False", ")", ":", "if", "not", "self", ".", "config_files_", "[", "index", ":", "]", ":", "return", "path", "=", "self", ".", "config_files_", "[", "index", "]", "if", "not", "path", ".", "parent", ".", "exists", "(", ")", ":", "path", ".", "parent", ".", "mkdir", "(", "parents", "=", "True", ")", "conf_dict", "=", "{", "}", "for", "section", "in", "self", ".", "sections_", "(", ")", ":", "conf_opts", "=", "[", "o", "for", "o", ",", "m", "in", "self", "[", "section", "]", ".", "defaults_", "(", ")", "if", "m", ".", "conf_arg", "]", "if", "not", "conf_opts", ":", "continue", "conf_dict", "[", "section", "]", "=", "{", "}", "for", "opt", "in", "conf_opts", ":", "conf_dict", "[", "section", "]", "[", "opt", "]", "=", "(", "self", "[", "section", "]", "[", "opt", "]", "if", "update", "else", "self", "[", "section", "]", ".", "def_", "[", "opt", "]", ".", "default", ")", "with", "path", ".", "open", "(", "'w'", ")", "as", "cfile", ":", "toml", ".", "dump", "(", "conf_dict", ",", "cfile", ")" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
ConfigurationManager.update_
Update values of configuration options with dict. Args: conf_dict (dict): dict of dict indexed with section and option names. conf_arg (bool): if True, only options that can be set in a config file are updated.
loam/manager.py
def update_(self, conf_dict, conf_arg=True): """Update values of configuration options with dict. Args: conf_dict (dict): dict of dict indexed with section and option names. conf_arg (bool): if True, only options that can be set in a config file are updated. """ for section, secdict in conf_dict.items(): self[section].update_(secdict, conf_arg)
def update_(self, conf_dict, conf_arg=True): """Update values of configuration options with dict. Args: conf_dict (dict): dict of dict indexed with section and option names. conf_arg (bool): if True, only options that can be set in a config file are updated. """ for section, secdict in conf_dict.items(): self[section].update_(secdict, conf_arg)
[ "Update", "values", "of", "configuration", "options", "with", "dict", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L339-L349
[ "def", "update_", "(", "self", ",", "conf_dict", ",", "conf_arg", "=", "True", ")", ":", "for", "section", ",", "secdict", "in", "conf_dict", ".", "items", "(", ")", ":", "self", "[", "section", "]", ".", "update_", "(", "secdict", ",", "conf_arg", ")" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
ConfigurationManager.read_config_
Read a config file and set config values accordingly. Returns: dict: content of config file.
loam/manager.py
def read_config_(self, cfile): """Read a config file and set config values accordingly. Returns: dict: content of config file. """ if not cfile.exists(): return {} try: conf_dict = toml.load(str(cfile)) except toml.TomlDecodeError: return None self.update_(conf_dict) return conf_dict
def read_config_(self, cfile): """Read a config file and set config values accordingly. Returns: dict: content of config file. """ if not cfile.exists(): return {} try: conf_dict = toml.load(str(cfile)) except toml.TomlDecodeError: return None self.update_(conf_dict) return conf_dict
[ "Read", "a", "config", "file", "and", "set", "config", "values", "accordingly", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L351-L364
[ "def", "read_config_", "(", "self", ",", "cfile", ")", ":", "if", "not", "cfile", ".", "exists", "(", ")", ":", "return", "{", "}", "try", ":", "conf_dict", "=", "toml", ".", "load", "(", "str", "(", "cfile", ")", ")", "except", "toml", ".", "TomlDecodeError", ":", "return", "None", "self", ".", "update_", "(", "conf_dict", ")", "return", "conf_dict" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
ConfigurationManager.read_configs_
Read config files and set config values accordingly. Returns: (dict, list, list): respectively content of files, list of missing/empty files and list of files for which a parsing error arised.
loam/manager.py
def read_configs_(self): """Read config files and set config values accordingly. Returns: (dict, list, list): respectively content of files, list of missing/empty files and list of files for which a parsing error arised. """ if not self.config_files_: return {}, [], [] content = {section: {} for section in self} empty_files = [] faulty_files = [] for cfile in self.config_files_: conf_dict = self.read_config_(cfile) if conf_dict is None: faulty_files.append(cfile) continue elif not conf_dict: empty_files.append(cfile) continue for section, secdict in conf_dict.items(): content[section].update(secdict) return content, empty_files, faulty_files
def read_configs_(self): """Read config files and set config values accordingly. Returns: (dict, list, list): respectively content of files, list of missing/empty files and list of files for which a parsing error arised. """ if not self.config_files_: return {}, [], [] content = {section: {} for section in self} empty_files = [] faulty_files = [] for cfile in self.config_files_: conf_dict = self.read_config_(cfile) if conf_dict is None: faulty_files.append(cfile) continue elif not conf_dict: empty_files.append(cfile) continue for section, secdict in conf_dict.items(): content[section].update(secdict) return content, empty_files, faulty_files
[ "Read", "config", "files", "and", "set", "config", "values", "accordingly", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/manager.py#L366-L389
[ "def", "read_configs_", "(", "self", ")", ":", "if", "not", "self", ".", "config_files_", ":", "return", "{", "}", ",", "[", "]", ",", "[", "]", "content", "=", "{", "section", ":", "{", "}", "for", "section", "in", "self", "}", "empty_files", "=", "[", "]", "faulty_files", "=", "[", "]", "for", "cfile", "in", "self", ".", "config_files_", ":", "conf_dict", "=", "self", ".", "read_config_", "(", "cfile", ")", "if", "conf_dict", "is", "None", ":", "faulty_files", ".", "append", "(", "cfile", ")", "continue", "elif", "not", "conf_dict", ":", "empty_files", ".", "append", "(", "cfile", ")", "continue", "for", "section", ",", "secdict", "in", "conf_dict", ".", "items", "(", ")", ":", "content", "[", "section", "]", ".", "update", "(", "secdict", ")", "return", "content", ",", "empty_files", ",", "faulty_files" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
_lookup_version
For the given module file (usually found by: from package import __file__ as module_file in the caller, return the location of the current RELEASE-VERSION file and the file itself.
yaclifw/version.py
def _lookup_version(module_file): """ For the given module file (usually found by: from package import __file__ as module_file in the caller, return the location of the current RELEASE-VERSION file and the file itself. """ version_dir = path.abspath(path.dirname(module_file)) version_file = path.join(version_dir, "RELEASE-VERSION") return version_dir, version_file
def _lookup_version(module_file): """ For the given module file (usually found by: from package import __file__ as module_file in the caller, return the location of the current RELEASE-VERSION file and the file itself. """ version_dir = path.abspath(path.dirname(module_file)) version_file = path.join(version_dir, "RELEASE-VERSION") return version_dir, version_file
[ "For", "the", "given", "module", "file", "(", "usually", "found", "by", ":" ]
openmicroscopy/yaclifw
python
https://github.com/openmicroscopy/yaclifw/blob/a01179fefb2c2c4260c75e6d1dc6e19de9979d64/yaclifw/version.py#L45-L57
[ "def", "_lookup_version", "(", "module_file", ")", ":", "version_dir", "=", "path", ".", "abspath", "(", "path", ".", "dirname", "(", "module_file", ")", ")", "version_file", "=", "path", ".", "join", "(", "version_dir", ",", "\"RELEASE-VERSION\"", ")", "return", "version_dir", ",", "version_file" ]
a01179fefb2c2c4260c75e6d1dc6e19de9979d64
test
_names
List of cli strings for a given option.
loam/cli.py
def _names(section, option): """List of cli strings for a given option.""" meta = section.def_[option] action = meta.cmd_kwargs.get('action') if action is internal.Switch: names = ['-{}'.format(option), '+{}'.format(option)] if meta.shortname is not None: names.append('-{}'.format(meta.shortname)) names.append('+{}'.format(meta.shortname)) else: names = ['--{}'.format(option)] if meta.shortname is not None: names.append('-{}'.format(meta.shortname)) return names
def _names(section, option): """List of cli strings for a given option.""" meta = section.def_[option] action = meta.cmd_kwargs.get('action') if action is internal.Switch: names = ['-{}'.format(option), '+{}'.format(option)] if meta.shortname is not None: names.append('-{}'.format(meta.shortname)) names.append('+{}'.format(meta.shortname)) else: names = ['--{}'.format(option)] if meta.shortname is not None: names.append('-{}'.format(meta.shortname)) return names
[ "List", "of", "cli", "strings", "for", "a", "given", "option", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/cli.py#L14-L27
[ "def", "_names", "(", "section", ",", "option", ")", ":", "meta", "=", "section", ".", "def_", "[", "option", "]", "action", "=", "meta", ".", "cmd_kwargs", ".", "get", "(", "'action'", ")", "if", "action", "is", "internal", ".", "Switch", ":", "names", "=", "[", "'-{}'", ".", "format", "(", "option", ")", ",", "'+{}'", ".", "format", "(", "option", ")", "]", "if", "meta", ".", "shortname", "is", "not", "None", ":", "names", ".", "append", "(", "'-{}'", ".", "format", "(", "meta", ".", "shortname", ")", ")", "names", ".", "append", "(", "'+{}'", ".", "format", "(", "meta", ".", "shortname", ")", ")", "else", ":", "names", "=", "[", "'--{}'", ".", "format", "(", "option", ")", "]", "if", "meta", ".", "shortname", "is", "not", "None", ":", "names", ".", "append", "(", "'-{}'", ".", "format", "(", "meta", ".", "shortname", ")", ")", "return", "names" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
CLIManager.sections_list
List of config sections used by a command. Args: cmd (str): command name, set to ``None`` or ``''`` for the bare command. Returns: list of str: list of configuration sections used by that command.
loam/cli.py
def sections_list(self, cmd=None): """List of config sections used by a command. Args: cmd (str): command name, set to ``None`` or ``''`` for the bare command. Returns: list of str: list of configuration sections used by that command. """ sections = list(self.common.sections) if not cmd: if self.bare is not None: sections.extend(self.bare.sections) return sections return [] sections.extend(self.subcmds[cmd].sections) if cmd in self._conf: sections.append(cmd) return sections
def sections_list(self, cmd=None): """List of config sections used by a command. Args: cmd (str): command name, set to ``None`` or ``''`` for the bare command. Returns: list of str: list of configuration sections used by that command. """ sections = list(self.common.sections) if not cmd: if self.bare is not None: sections.extend(self.bare.sections) return sections return [] sections.extend(self.subcmds[cmd].sections) if cmd in self._conf: sections.append(cmd) return sections
[ "List", "of", "config", "sections", "used", "by", "a", "command", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/cli.py#L104-L123
[ "def", "sections_list", "(", "self", ",", "cmd", "=", "None", ")", ":", "sections", "=", "list", "(", "self", ".", "common", ".", "sections", ")", "if", "not", "cmd", ":", "if", "self", ".", "bare", "is", "not", "None", ":", "sections", ".", "extend", "(", "self", ".", "bare", ".", "sections", ")", "return", "sections", "return", "[", "]", "sections", ".", "extend", "(", "self", ".", "subcmds", "[", "cmd", "]", ".", "sections", ")", "if", "cmd", "in", "self", ".", "_conf", ":", "sections", ".", "append", "(", "cmd", ")", "return", "sections" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
CLIManager._cmd_opts_solver
Scan options related to one command and enrich _opt_cmds.
loam/cli.py
def _cmd_opts_solver(self, cmd_name): """Scan options related to one command and enrich _opt_cmds.""" sections = self.sections_list(cmd_name) cmd_dict = self._opt_cmds[cmd_name] if cmd_name else self._opt_bare for sct in reversed(sections): for opt, opt_meta in self._conf[sct].def_.items(): if not opt_meta.cmd_arg: continue if opt not in cmd_dict: cmd_dict[opt] = sct else: warnings.warn( 'Command <{0}>: {1}.{2} shadowed by {3}.{2}'.format( cmd_name, sct, opt, cmd_dict[opt]), error.LoamWarning, stacklevel=4)
def _cmd_opts_solver(self, cmd_name): """Scan options related to one command and enrich _opt_cmds.""" sections = self.sections_list(cmd_name) cmd_dict = self._opt_cmds[cmd_name] if cmd_name else self._opt_bare for sct in reversed(sections): for opt, opt_meta in self._conf[sct].def_.items(): if not opt_meta.cmd_arg: continue if opt not in cmd_dict: cmd_dict[opt] = sct else: warnings.warn( 'Command <{0}>: {1}.{2} shadowed by {3}.{2}'.format( cmd_name, sct, opt, cmd_dict[opt]), error.LoamWarning, stacklevel=4)
[ "Scan", "options", "related", "to", "one", "command", "and", "enrich", "_opt_cmds", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/cli.py#L125-L139
[ "def", "_cmd_opts_solver", "(", "self", ",", "cmd_name", ")", ":", "sections", "=", "self", ".", "sections_list", "(", "cmd_name", ")", "cmd_dict", "=", "self", ".", "_opt_cmds", "[", "cmd_name", "]", "if", "cmd_name", "else", "self", ".", "_opt_bare", "for", "sct", "in", "reversed", "(", "sections", ")", ":", "for", "opt", ",", "opt_meta", "in", "self", ".", "_conf", "[", "sct", "]", ".", "def_", ".", "items", "(", ")", ":", "if", "not", "opt_meta", ".", "cmd_arg", ":", "continue", "if", "opt", "not", "in", "cmd_dict", ":", "cmd_dict", "[", "opt", "]", "=", "sct", "else", ":", "warnings", ".", "warn", "(", "'Command <{0}>: {1}.{2} shadowed by {3}.{2}'", ".", "format", "(", "cmd_name", ",", "sct", ",", "opt", ",", "cmd_dict", "[", "opt", "]", ")", ",", "error", ".", "LoamWarning", ",", "stacklevel", "=", "4", ")" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
CLIManager._add_options_to_parser
Add options to a parser.
loam/cli.py
def _add_options_to_parser(self, opts_dict, parser): """Add options to a parser.""" store_bool = ('store_true', 'store_false') for opt, sct in opts_dict.items(): meta = self._conf[sct].def_[opt] kwargs = copy.deepcopy(meta.cmd_kwargs) action = kwargs.get('action') if action is internal.Switch: kwargs.update(nargs=0) elif meta.default is not None and action not in store_bool: kwargs.setdefault('type', type(meta.default)) kwargs.update(help=meta.help) kwargs.setdefault('default', self._conf[sct][opt]) parser.add_argument(*_names(self._conf[sct], opt), **kwargs)
def _add_options_to_parser(self, opts_dict, parser): """Add options to a parser.""" store_bool = ('store_true', 'store_false') for opt, sct in opts_dict.items(): meta = self._conf[sct].def_[opt] kwargs = copy.deepcopy(meta.cmd_kwargs) action = kwargs.get('action') if action is internal.Switch: kwargs.update(nargs=0) elif meta.default is not None and action not in store_bool: kwargs.setdefault('type', type(meta.default)) kwargs.update(help=meta.help) kwargs.setdefault('default', self._conf[sct][opt]) parser.add_argument(*_names(self._conf[sct], opt), **kwargs)
[ "Add", "options", "to", "a", "parser", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/cli.py#L141-L154
[ "def", "_add_options_to_parser", "(", "self", ",", "opts_dict", ",", "parser", ")", ":", "store_bool", "=", "(", "'store_true'", ",", "'store_false'", ")", "for", "opt", ",", "sct", "in", "opts_dict", ".", "items", "(", ")", ":", "meta", "=", "self", ".", "_conf", "[", "sct", "]", ".", "def_", "[", "opt", "]", "kwargs", "=", "copy", ".", "deepcopy", "(", "meta", ".", "cmd_kwargs", ")", "action", "=", "kwargs", ".", "get", "(", "'action'", ")", "if", "action", "is", "internal", ".", "Switch", ":", "kwargs", ".", "update", "(", "nargs", "=", "0", ")", "elif", "meta", ".", "default", "is", "not", "None", "and", "action", "not", "in", "store_bool", ":", "kwargs", ".", "setdefault", "(", "'type'", ",", "type", "(", "meta", ".", "default", ")", ")", "kwargs", ".", "update", "(", "help", "=", "meta", ".", "help", ")", "kwargs", ".", "setdefault", "(", "'default'", ",", "self", ".", "_conf", "[", "sct", "]", "[", "opt", "]", ")", "parser", ".", "add_argument", "(", "*", "_names", "(", "self", ".", "_conf", "[", "sct", "]", ",", "opt", ")", ",", "*", "*", "kwargs", ")" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
CLIManager._build_parser
Build command line argument parser. Returns: :class:`argparse.ArgumentParser`: the command line argument parser. You probably won't need to use it directly. To parse command line arguments and update the :class:`ConfigurationManager` instance accordingly, use the :meth:`parse_args` method.
loam/cli.py
def _build_parser(self): """Build command line argument parser. Returns: :class:`argparse.ArgumentParser`: the command line argument parser. You probably won't need to use it directly. To parse command line arguments and update the :class:`ConfigurationManager` instance accordingly, use the :meth:`parse_args` method. """ main_parser = argparse.ArgumentParser(description=self.common.help, prefix_chars='-+') self._add_options_to_parser(self._opt_bare, main_parser) main_parser.set_defaults(**self.common.defaults) if self.bare is not None: main_parser.set_defaults(**self.bare.defaults) subparsers = main_parser.add_subparsers(dest='loam_sub_name') for cmd_name, meta in self.subcmds.items(): kwargs = {'prefix_chars': '+-', 'help': meta.help} dummy_parser = subparsers.add_parser(cmd_name, **kwargs) self._add_options_to_parser(self._opt_cmds[cmd_name], dummy_parser) dummy_parser.set_defaults(**meta.defaults) return main_parser
def _build_parser(self): """Build command line argument parser. Returns: :class:`argparse.ArgumentParser`: the command line argument parser. You probably won't need to use it directly. To parse command line arguments and update the :class:`ConfigurationManager` instance accordingly, use the :meth:`parse_args` method. """ main_parser = argparse.ArgumentParser(description=self.common.help, prefix_chars='-+') self._add_options_to_parser(self._opt_bare, main_parser) main_parser.set_defaults(**self.common.defaults) if self.bare is not None: main_parser.set_defaults(**self.bare.defaults) subparsers = main_parser.add_subparsers(dest='loam_sub_name') for cmd_name, meta in self.subcmds.items(): kwargs = {'prefix_chars': '+-', 'help': meta.help} dummy_parser = subparsers.add_parser(cmd_name, **kwargs) self._add_options_to_parser(self._opt_cmds[cmd_name], dummy_parser) dummy_parser.set_defaults(**meta.defaults) return main_parser
[ "Build", "command", "line", "argument", "parser", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/cli.py#L156-L180
[ "def", "_build_parser", "(", "self", ")", ":", "main_parser", "=", "argparse", ".", "ArgumentParser", "(", "description", "=", "self", ".", "common", ".", "help", ",", "prefix_chars", "=", "'-+'", ")", "self", ".", "_add_options_to_parser", "(", "self", ".", "_opt_bare", ",", "main_parser", ")", "main_parser", ".", "set_defaults", "(", "*", "*", "self", ".", "common", ".", "defaults", ")", "if", "self", ".", "bare", "is", "not", "None", ":", "main_parser", ".", "set_defaults", "(", "*", "*", "self", ".", "bare", ".", "defaults", ")", "subparsers", "=", "main_parser", ".", "add_subparsers", "(", "dest", "=", "'loam_sub_name'", ")", "for", "cmd_name", ",", "meta", "in", "self", ".", "subcmds", ".", "items", "(", ")", ":", "kwargs", "=", "{", "'prefix_chars'", ":", "'+-'", ",", "'help'", ":", "meta", ".", "help", "}", "dummy_parser", "=", "subparsers", ".", "add_parser", "(", "cmd_name", ",", "*", "*", "kwargs", ")", "self", ".", "_add_options_to_parser", "(", "self", ".", "_opt_cmds", "[", "cmd_name", "]", ",", "dummy_parser", ")", "dummy_parser", ".", "set_defaults", "(", "*", "*", "meta", ".", "defaults", ")", "return", "main_parser" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
CLIManager.parse_args
Parse arguments and update options accordingly. Args: arglist (list of str): list of arguments to parse. If set to None, ``sys.argv[1:]`` is used. Returns: :class:`Namespace`: the argument namespace returned by the :class:`argparse.ArgumentParser`.
loam/cli.py
def parse_args(self, arglist=None): """Parse arguments and update options accordingly. Args: arglist (list of str): list of arguments to parse. If set to None, ``sys.argv[1:]`` is used. Returns: :class:`Namespace`: the argument namespace returned by the :class:`argparse.ArgumentParser`. """ args = self._parser.parse_args(args=arglist) sub_cmd = args.loam_sub_name if sub_cmd is None: for opt, sct in self._opt_bare.items(): self._conf[sct][opt] = getattr(args, opt, None) else: for opt, sct in self._opt_cmds[sub_cmd].items(): self._conf[sct][opt] = getattr(args, opt, None) return args
def parse_args(self, arglist=None): """Parse arguments and update options accordingly. Args: arglist (list of str): list of arguments to parse. If set to None, ``sys.argv[1:]`` is used. Returns: :class:`Namespace`: the argument namespace returned by the :class:`argparse.ArgumentParser`. """ args = self._parser.parse_args(args=arglist) sub_cmd = args.loam_sub_name if sub_cmd is None: for opt, sct in self._opt_bare.items(): self._conf[sct][opt] = getattr(args, opt, None) else: for opt, sct in self._opt_cmds[sub_cmd].items(): self._conf[sct][opt] = getattr(args, opt, None) return args
[ "Parse", "arguments", "and", "update", "options", "accordingly", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/cli.py#L182-L201
[ "def", "parse_args", "(", "self", ",", "arglist", "=", "None", ")", ":", "args", "=", "self", ".", "_parser", ".", "parse_args", "(", "args", "=", "arglist", ")", "sub_cmd", "=", "args", ".", "loam_sub_name", "if", "sub_cmd", "is", "None", ":", "for", "opt", ",", "sct", "in", "self", ".", "_opt_bare", ".", "items", "(", ")", ":", "self", ".", "_conf", "[", "sct", "]", "[", "opt", "]", "=", "getattr", "(", "args", ",", "opt", ",", "None", ")", "else", ":", "for", "opt", ",", "sct", "in", "self", ".", "_opt_cmds", "[", "sub_cmd", "]", ".", "items", "(", ")", ":", "self", ".", "_conf", "[", "sct", "]", "[", "opt", "]", "=", "getattr", "(", "args", ",", "opt", ",", "None", ")", "return", "args" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
CLIManager._zsh_comp_command
Write zsh _arguments compdef for a given command. Args: zcf (file): zsh compdef file. cmd (str): command name, set to None or '' for bare command. grouping (bool): group options (zsh>=5.4). add_help (bool): add an help option.
loam/cli.py
def _zsh_comp_command(self, zcf, cmd, grouping, add_help=True): """Write zsh _arguments compdef for a given command. Args: zcf (file): zsh compdef file. cmd (str): command name, set to None or '' for bare command. grouping (bool): group options (zsh>=5.4). add_help (bool): add an help option. """ if add_help: if grouping: print("+ '(help)'", end=BLK, file=zcf) print("'--help[show help message]'", end=BLK, file=zcf) print("'-h[show help message]'", end=BLK, file=zcf) # could deal with duplicate by iterating in reverse and keep set of # already defined opts. no_comp = ('store_true', 'store_false') cmd_dict = self._opt_cmds[cmd] if cmd else self._opt_bare for opt, sct in cmd_dict.items(): meta = self._conf[sct].def_[opt] if meta.cmd_kwargs.get('action') == 'append': grpfmt, optfmt = "+ '{}'", "'*{}[{}]{}'" if meta.comprule is None: meta.comprule = '' else: grpfmt, optfmt = "+ '({})'", "'{}[{}]{}'" if meta.cmd_kwargs.get('action') in no_comp \ or meta.cmd_kwargs.get('nargs') == 0: meta.comprule = None if meta.comprule is None: compstr = '' elif meta.comprule == '': optfmt = optfmt.split('[') optfmt = optfmt[0] + '=[' + optfmt[1] compstr = ': :( )' else: optfmt = optfmt.split('[') optfmt = optfmt[0] + '=[' + optfmt[1] compstr = ': :{}'.format(meta.comprule) if grouping: print(grpfmt.format(opt), end=BLK, file=zcf) for name in _names(self._conf[sct], opt): print(optfmt.format(name, meta.help.replace("'", "'\"'\"'"), compstr), end=BLK, file=zcf)
def _zsh_comp_command(self, zcf, cmd, grouping, add_help=True): """Write zsh _arguments compdef for a given command. Args: zcf (file): zsh compdef file. cmd (str): command name, set to None or '' for bare command. grouping (bool): group options (zsh>=5.4). add_help (bool): add an help option. """ if add_help: if grouping: print("+ '(help)'", end=BLK, file=zcf) print("'--help[show help message]'", end=BLK, file=zcf) print("'-h[show help message]'", end=BLK, file=zcf) # could deal with duplicate by iterating in reverse and keep set of # already defined opts. no_comp = ('store_true', 'store_false') cmd_dict = self._opt_cmds[cmd] if cmd else self._opt_bare for opt, sct in cmd_dict.items(): meta = self._conf[sct].def_[opt] if meta.cmd_kwargs.get('action') == 'append': grpfmt, optfmt = "+ '{}'", "'*{}[{}]{}'" if meta.comprule is None: meta.comprule = '' else: grpfmt, optfmt = "+ '({})'", "'{}[{}]{}'" if meta.cmd_kwargs.get('action') in no_comp \ or meta.cmd_kwargs.get('nargs') == 0: meta.comprule = None if meta.comprule is None: compstr = '' elif meta.comprule == '': optfmt = optfmt.split('[') optfmt = optfmt[0] + '=[' + optfmt[1] compstr = ': :( )' else: optfmt = optfmt.split('[') optfmt = optfmt[0] + '=[' + optfmt[1] compstr = ': :{}'.format(meta.comprule) if grouping: print(grpfmt.format(opt), end=BLK, file=zcf) for name in _names(self._conf[sct], opt): print(optfmt.format(name, meta.help.replace("'", "'\"'\"'"), compstr), end=BLK, file=zcf)
[ "Write", "zsh", "_arguments", "compdef", "for", "a", "given", "command", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/cli.py#L203-L246
[ "def", "_zsh_comp_command", "(", "self", ",", "zcf", ",", "cmd", ",", "grouping", ",", "add_help", "=", "True", ")", ":", "if", "add_help", ":", "if", "grouping", ":", "print", "(", "\"+ '(help)'\"", ",", "end", "=", "BLK", ",", "file", "=", "zcf", ")", "print", "(", "\"'--help[show help message]'\"", ",", "end", "=", "BLK", ",", "file", "=", "zcf", ")", "print", "(", "\"'-h[show help message]'\"", ",", "end", "=", "BLK", ",", "file", "=", "zcf", ")", "# could deal with duplicate by iterating in reverse and keep set of", "# already defined opts.", "no_comp", "=", "(", "'store_true'", ",", "'store_false'", ")", "cmd_dict", "=", "self", ".", "_opt_cmds", "[", "cmd", "]", "if", "cmd", "else", "self", ".", "_opt_bare", "for", "opt", ",", "sct", "in", "cmd_dict", ".", "items", "(", ")", ":", "meta", "=", "self", ".", "_conf", "[", "sct", "]", ".", "def_", "[", "opt", "]", "if", "meta", ".", "cmd_kwargs", ".", "get", "(", "'action'", ")", "==", "'append'", ":", "grpfmt", ",", "optfmt", "=", "\"+ '{}'\"", ",", "\"'*{}[{}]{}'\"", "if", "meta", ".", "comprule", "is", "None", ":", "meta", ".", "comprule", "=", "''", "else", ":", "grpfmt", ",", "optfmt", "=", "\"+ '({})'\"", ",", "\"'{}[{}]{}'\"", "if", "meta", ".", "cmd_kwargs", ".", "get", "(", "'action'", ")", "in", "no_comp", "or", "meta", ".", "cmd_kwargs", ".", "get", "(", "'nargs'", ")", "==", "0", ":", "meta", ".", "comprule", "=", "None", "if", "meta", ".", "comprule", "is", "None", ":", "compstr", "=", "''", "elif", "meta", ".", "comprule", "==", "''", ":", "optfmt", "=", "optfmt", ".", "split", "(", "'['", ")", "optfmt", "=", "optfmt", "[", "0", "]", "+", "'=['", "+", "optfmt", "[", "1", "]", "compstr", "=", "': :( )'", "else", ":", "optfmt", "=", "optfmt", ".", "split", "(", "'['", ")", "optfmt", "=", "optfmt", "[", "0", "]", "+", "'=['", "+", "optfmt", "[", "1", "]", "compstr", "=", "': :{}'", ".", "format", "(", "meta", ".", "comprule", ")", "if", "grouping", ":", "print", "(", "grpfmt", ".", "format", "(", "opt", ")", ",", "end", "=", "BLK", ",", "file", "=", "zcf", ")", "for", "name", "in", "_names", "(", "self", ".", "_conf", "[", "sct", "]", ",", "opt", ")", ":", "print", "(", "optfmt", ".", "format", "(", "name", ",", "meta", ".", "help", ".", "replace", "(", "\"'\"", ",", "\"'\\\"'\\\"'\"", ")", ",", "compstr", ")", ",", "end", "=", "BLK", ",", "file", "=", "zcf", ")" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94
test
CLIManager.zsh_complete
Write zsh compdef script. Args: path (path-like): desired path of the compdef script. cmd (str): command name that should be completed. cmds (str): extra command names that should be completed. sourceable (bool): if True, the generated file will contain an explicit call to ``compdef``, which means it can be sourced to activate CLI completion.
loam/cli.py
def zsh_complete(self, path, cmd, *cmds, sourceable=False): """Write zsh compdef script. Args: path (path-like): desired path of the compdef script. cmd (str): command name that should be completed. cmds (str): extra command names that should be completed. sourceable (bool): if True, the generated file will contain an explicit call to ``compdef``, which means it can be sourced to activate CLI completion. """ grouping = internal.zsh_version() >= (5, 4) path = pathlib.Path(path) firstline = ['#compdef', cmd] firstline.extend(cmds) subcmds = list(self.subcmds.keys()) with path.open('w') as zcf: print(*firstline, end='\n\n', file=zcf) # main function print('function _{} {{'.format(cmd), file=zcf) print('local line', file=zcf) print('_arguments -C', end=BLK, file=zcf) if subcmds: # list of subcommands and their description substrs = ["{}\\:'{}'".format(sub, self.subcmds[sub].help) for sub in subcmds] print('"1:Commands:(({}))"'.format(' '.join(substrs)), end=BLK, file=zcf) self._zsh_comp_command(zcf, None, grouping) if subcmds: print("'*::arg:->args'", file=zcf) print('case $line[1] in', file=zcf) for sub in subcmds: print('{sub}) _{cmd}_{sub} ;;'.format(sub=sub, cmd=cmd), file=zcf) print('esac', file=zcf) print('}', file=zcf) # all subcommand completion handlers for sub in subcmds: print('\nfunction _{}_{} {{'.format(cmd, sub), file=zcf) print('_arguments', end=BLK, file=zcf) self._zsh_comp_command(zcf, sub, grouping) print('}', file=zcf) if sourceable: print('\ncompdef _{0} {0}'.format(cmd), *cmds, file=zcf)
def zsh_complete(self, path, cmd, *cmds, sourceable=False): """Write zsh compdef script. Args: path (path-like): desired path of the compdef script. cmd (str): command name that should be completed. cmds (str): extra command names that should be completed. sourceable (bool): if True, the generated file will contain an explicit call to ``compdef``, which means it can be sourced to activate CLI completion. """ grouping = internal.zsh_version() >= (5, 4) path = pathlib.Path(path) firstline = ['#compdef', cmd] firstline.extend(cmds) subcmds = list(self.subcmds.keys()) with path.open('w') as zcf: print(*firstline, end='\n\n', file=zcf) # main function print('function _{} {{'.format(cmd), file=zcf) print('local line', file=zcf) print('_arguments -C', end=BLK, file=zcf) if subcmds: # list of subcommands and their description substrs = ["{}\\:'{}'".format(sub, self.subcmds[sub].help) for sub in subcmds] print('"1:Commands:(({}))"'.format(' '.join(substrs)), end=BLK, file=zcf) self._zsh_comp_command(zcf, None, grouping) if subcmds: print("'*::arg:->args'", file=zcf) print('case $line[1] in', file=zcf) for sub in subcmds: print('{sub}) _{cmd}_{sub} ;;'.format(sub=sub, cmd=cmd), file=zcf) print('esac', file=zcf) print('}', file=zcf) # all subcommand completion handlers for sub in subcmds: print('\nfunction _{}_{} {{'.format(cmd, sub), file=zcf) print('_arguments', end=BLK, file=zcf) self._zsh_comp_command(zcf, sub, grouping) print('}', file=zcf) if sourceable: print('\ncompdef _{0} {0}'.format(cmd), *cmds, file=zcf)
[ "Write", "zsh", "compdef", "script", "." ]
amorison/loam
python
https://github.com/amorison/loam/blob/a566c943a75e068a4510099331a1ddfe5bbbdd94/loam/cli.py#L248-L292
[ "def", "zsh_complete", "(", "self", ",", "path", ",", "cmd", ",", "*", "cmds", ",", "sourceable", "=", "False", ")", ":", "grouping", "=", "internal", ".", "zsh_version", "(", ")", ">=", "(", "5", ",", "4", ")", "path", "=", "pathlib", ".", "Path", "(", "path", ")", "firstline", "=", "[", "'#compdef'", ",", "cmd", "]", "firstline", ".", "extend", "(", "cmds", ")", "subcmds", "=", "list", "(", "self", ".", "subcmds", ".", "keys", "(", ")", ")", "with", "path", ".", "open", "(", "'w'", ")", "as", "zcf", ":", "print", "(", "*", "firstline", ",", "end", "=", "'\\n\\n'", ",", "file", "=", "zcf", ")", "# main function", "print", "(", "'function _{} {{'", ".", "format", "(", "cmd", ")", ",", "file", "=", "zcf", ")", "print", "(", "'local line'", ",", "file", "=", "zcf", ")", "print", "(", "'_arguments -C'", ",", "end", "=", "BLK", ",", "file", "=", "zcf", ")", "if", "subcmds", ":", "# list of subcommands and their description", "substrs", "=", "[", "\"{}\\\\:'{}'\"", ".", "format", "(", "sub", ",", "self", ".", "subcmds", "[", "sub", "]", ".", "help", ")", "for", "sub", "in", "subcmds", "]", "print", "(", "'\"1:Commands:(({}))\"'", ".", "format", "(", "' '", ".", "join", "(", "substrs", ")", ")", ",", "end", "=", "BLK", ",", "file", "=", "zcf", ")", "self", ".", "_zsh_comp_command", "(", "zcf", ",", "None", ",", "grouping", ")", "if", "subcmds", ":", "print", "(", "\"'*::arg:->args'\"", ",", "file", "=", "zcf", ")", "print", "(", "'case $line[1] in'", ",", "file", "=", "zcf", ")", "for", "sub", "in", "subcmds", ":", "print", "(", "'{sub}) _{cmd}_{sub} ;;'", ".", "format", "(", "sub", "=", "sub", ",", "cmd", "=", "cmd", ")", ",", "file", "=", "zcf", ")", "print", "(", "'esac'", ",", "file", "=", "zcf", ")", "print", "(", "'}'", ",", "file", "=", "zcf", ")", "# all subcommand completion handlers", "for", "sub", "in", "subcmds", ":", "print", "(", "'\\nfunction _{}_{} {{'", ".", "format", "(", "cmd", ",", "sub", ")", ",", "file", "=", "zcf", ")", "print", "(", "'_arguments'", ",", "end", "=", "BLK", ",", "file", "=", "zcf", ")", "self", ".", "_zsh_comp_command", "(", "zcf", ",", "sub", ",", "grouping", ")", "print", "(", "'}'", ",", "file", "=", "zcf", ")", "if", "sourceable", ":", "print", "(", "'\\ncompdef _{0} {0}'", ".", "format", "(", "cmd", ")", ",", "*", "cmds", ",", "file", "=", "zcf", ")" ]
a566c943a75e068a4510099331a1ddfe5bbbdd94