def __init__(self, name="", models=[], biomass=[], carbon_sources=[]): self.name = name self.models = util.IntelliContainer() for id, biomass, carbon_source in zip(models, biomass, carbon_sources): model = Proxy(partial(load_model, os.path.join(MODEL_DIRECTORY, id + ".xml"))) model.biomass = biomass model.carbon_source = carbon_source self.models[id] = model
def test_proxy(self): from lazy_object_proxy import Proxy def unwrap(): return "foo" data = {"args": [Proxy(unwrap)]} expected = '{"args":["foo"]}' actual = json_dumps(data) self.assertEqual(expected, actual)
def u(http): """ Mock UCS updater. """ http({ '': b'', '/': b'', RJSON: b'{"releases":[]}', }) return Proxy(U.UniventionUpdater)
def __init__(self, name='', models=[], biomass=[], carbon_sources=[]): self.name = name self.models = util.IntelliContainer() for id, biomass, carbon_source in zip(models, biomass, carbon_sources): model = Proxy( partial(load_model, os.path.join(MODEL_DIRECTORY, id + '.json'))) setattr(model, "biomass", biomass) setattr(model, "carbon_source", carbon_source) self.models[id] = model
def api(self) -> PackitAPI: if not self._api: self._api = PackitAPI( self.service_config, self.job_config, # so that the local_project is evaluated only if needed Proxy(partial(BaseBuildJobHelper.local_project.__get__, self)), # type: ignore stage=self.service_config.use_stage(), ) return self._api
def __call__(self, func, *args, **kwargs): def wrapper(): if self._lock is not None: with self._lock: return self._get_func_result(func, *args, **kwargs) else: return self._get_func_result(func, *args, **kwargs) assert callable(func), "lazy_init must be called with callable object" if not hasattr(func, "__name__"): func.__name__ = "unknown_function" return Proxy(wrapper)
def get_or_create(self, schema_spec): schema_hash = dicthash(schema_spec) schema_deref = self.dereferencer.dereference(schema_spec) if schema_hash in self._schemas: return self._schemas[schema_hash], False if '$ref' in schema_spec: schema = Proxy(lambda: self.create(schema_deref)) else: schema = self.create(schema_deref) self._schemas[schema_hash] = schema return schema, True
def m(tmpdir, ucr, http): """ Mock UCS repository mirror. """ ucr({ 'repository/mirror/basepath': str(tmpdir / 'repo'), # 'repository/mirror/version/end': '%d.%d-%d' % (MAJOR, MINOR, PATCH), # 'repository/mirror/version/start': '%d.%d-%d' % (MAJOR, 0, 0), 'repository/mirror/verify': 'no', }) http({ # 'univention-repository/': '', '': b'', '/': b'', RJSON: b'{"releases":[]}', }) return Proxy(M.UniventionMirror)
def schemas(self): """ object: An object with attributes corresponding to the names of the schemas in this database. """ from lazy_object_proxy import Proxy def get_schemas(): if not getattr(self, '_schemas', None): assert getattr( self, '_sqlalchemy_metadata', None ) is not None, ( "`{class_name}` instances do not provide the required sqlalchemy metadata " "for schema exploration.".format(self.__class__.__name__)) self._schemas = Schemas(self._sqlalchemy_metadata) return self._schemas return Proxy(get_schemas)
def __getattr__(name): if name in ["TIMESTAMP", "StringID"]: def lazy_load(): dialect = context.get_bind().dialect.name module = globals() # Lookup the type based on the dialect specific type, or fallback to the generic type type_ = module.get(f'_{dialect}_{name}', None) or module.get(f'_sa_{name}') val = module[name] = type_() return val # Prior to v1.4 of our Helm chart we didn't correctly initialize the Migration environment, so # `context.get_bind()` would fail if called at the top level. To make it easier on migration writers # we make the returned objects lazy. return Proxy(lazy_load) raise AttributeError(f"module {__name__} has no attribute {name}")
def __init__(self, name='', models=None, biomass=None, carbon_sources=None): models = models or [] biomass = biomass or [] carbon_sources = carbon_sources or [] self.name = name self.models = util.IntelliContainer() for id, biomass, carbon_source in zip(models, biomass, carbon_sources): def lazy_model_init(path): model = load_model(path) setattr(model, "biomass", biomass) setattr(model, "carbon_source", carbon_source) return model model = Proxy( partial(lazy_model_init, os.path.join(MODEL_DIRECTORY, id + '.json'))) self.models[id] = model
def transform_attr( attr_spec: Attr, self, _transform=None, *, _inplace: bool = False, _if: bool = True, **attr_transforms, ): if not _if: return self return WithAttrMethod.with_attr( attr_spec, self, _new_value=mutate_value( old_value=Proxy( lambda: getattr(self, attr_spec.name, MISSING)), transform=_transform, constructor=attr_spec.type, attr_transforms=attr_transforms, ), _inplace=_inplace, )
def __getattribute__(self, name): # To see if name is an instanced attribute of Reference. Here we can # check the names of the class attributes and then return the instance # attribute with the same name. attr = Proxy.__getattribute__(self, name) if hasattr(Reference, name): return attr # Intercept all proxied attributes and methods and attempt to update # the Reference ._id and ._href attributes. def update_ref(item): try: id_ = item.id href_ = urljoin(analyzere.base_url, item._get_path(id_)) self._id = id_ self._href = href_ except AttributeError: pass # Check to see if the attribute is a method that is being called. if hasattr(attr, '__call__'): # Intercept class method for the Resources, as they should not be # able to update the _id and _href for a reference. Class methods # should never update an instance in place. if isclass(attr.__self__) and issubclass(attr.__self__, Resource): return attr def newfunc(*args, **kwargs): retval = attr(*args, **kwargs) update_ref(retval) return retval return newfunc else: return attr
# simple version in there too. ###################################### def _mssql_use_date_time2(): conn = context.get_bind() result = conn.execute( """SELECT CASE WHEN CONVERT(VARCHAR(128), SERVERPROPERTY ('productversion')) like '8%' THEN '2000' WHEN CONVERT(VARCHAR(128), SERVERPROPERTY ('productversion')) like '9%' THEN '2005' ELSE '2005Plus' END AS MajorVersion""").fetchone( ) mssql_version = result[0] return mssql_version not in ("2000", "2005") MSSQL_USE_DATE_TIME2 = Proxy(_mssql_use_date_time2) def _mssql_TIMESTAMP(): from sqlalchemy.dialects import mssql if MSSQL_USE_DATE_TIME2: class DATETIME2(mssql.DATETIME2): def __init__(self, *args, precision=6, **kwargs): super().__init__(*args, precision=precision, **kwargs) return DATETIME2 return mssql.DATETIME
This module borrows from the `rpchemtools <https://github.com/brsynth/rpchemtools>` toolbox by Jean-Loup Faulon's Lab. """ __all__ = ["standardize"] from lazy_object_proxy import Proxy from rdkit import Chem from rdkit.Chem import AllChem, Descriptors _NEUTRALIZE_PATTERNS = Proxy(lambda: [ (Chem.MolFromSmarts(charged_pat), Chem.MolFromSmiles(uncharged_pat, False)) for charged_pat, uncharged_pat in ( ("[n+;H]", "n"), # Imidazoles ("[N+;!H0]", "N"), # Amines ("[$([O-]);!$([O-][#7])]", "O"), # Carboxylic acids and alcohols ("[S-;X1]", "S"), # Thiols ("[$([N-;X2]S(=O)=O)]", "N"), # Sulfonamides ("[$([N-;X2][C,N]=C)]", "N"), # Enamines ("[n-]", "[nH]"), # Tetrazoles ("[$([S-]=O)]", "S"), # Sulfoxides ("[$([N-]C=O)]", "N"), # Amides ) ]) def _transfer_props(source, target): # transfer properties from one molecule to another props = source.GetPropNames(includePrivate=False) if source.HasProp("_Name"): props.append("_Name") for prop in props: target.SetProp(prop, source.GetProp(prop))
def get_project(self, url: str, get_project_kwargs: dict = None) -> GitProject: return Proxy(partial(self._get_project, url, get_project_kwargs))
tuner.tune() return tuner._parsed.instantiate(tuner._strategy.points[np.argmin( tuner._strategy.values)]) class HyperEngineBayesian(HyperEngine): hyperTunerStrategy = "bayesian" def createDefaultPortfolioMethods(): from hyperengine.bayesian.strategy import utilities return tuple(utilities.keys()) defaultPortfolioMethods = Proxy(createDefaultPortfolioMethods) class HyperEnginePortfolio(HyperEngine): hyperTunerStrategy = "portfolio" def __init__(self, blackBoxFunc: typing.Callable, spaceSpec: typing.Mapping[str, object], iters: int = 1000, jobs: int = 3, **strategy_params): if "methods" not in strategy_params: strategy_params["methods"] = defaultPortfolioMethods super().__init__(blackBoxFunc, spaceSpec, iters, jobs, **strategy_params)
_global_config = None def get_global_config(): """ Get the global config object. You may set the global config object by :func:`set_global_config`. Returns: The global config object. """ return _global_config global_config = Proxy(get_global_config) """ The proxy object to the global config. Usage:: from tfsnippet.examples.utils import MLConfig, global_config, set_global_config class YourConfig(MLConfig): max_epoch = 100 set_global_config(YourConfig()) # you may also use click + config_options assert(isinstance(global_config, YourConfig)) assert(global_config.max_epoch == 100) """
logging.register_options(cfg.CONF) LOG = logging.getLogger(__name__) def make_ironicclient(): return client.get_client( "1", os_username="******", os_password="******", os_tenant_name="admin", os_auth_url="http://172.27.59.42:5000/v2.0/", os_region_name="RegionOne" ) ironicclient = Proxy(make_ironicclient) def wire_stuff(app): resource_type_factory = ResourceTypeFactory() LOG.info("Configuring the resource factory from {}".format(cfg.CONF.type_definition_file)) resource_type_yaml_parser = ResourceTypeYamlParser(resource_type_factory) resource_type_yaml_parser.configure_factory_from(cfg.CONF.find_file(cfg.CONF.type_definition_file)) datastore = driver.DriverManager( 'cellar.datastores', cfg.CONF.datastore, invoke_on_load=True).driver Api(app,
def __new__(cls, *args, **kwargs): original_class = type(object.__name__, (cls,), {}) original_class.__new__ = lambda cls_, *args_, **kwargs_: object.__new__(cls_) return Proxy(lambda: original_class(*args, **kwargs))
@property def celery_app(self): if self._celery_app is None: password = getenv("REDIS_PASSWORD", "") host = getenv("REDIS_SERVICE_HOST", "redis") port = getenv("REDIS_SERVICE_PORT", "6379") db = getenv("REDIS_SERVICE_DB", "0") redis_url = f"redis://:{password}@{host}:{port}/{db}" # https://docs.celeryproject.org/en/stable/userguide/configuration.html#database-url-examples postgres_url = f"db+{get_pg_url()}" # http://docs.celeryproject.org/en/latest/reference/celery.html#celery.Celery self._celery_app = Celery(backend=postgres_url, broker=redis_url) return self._celery_app def get_celery_application(): celerizer = Celerizer() app = celerizer.celery_app configure_sentry( runner_type="packit-worker", celery_integration=True, sqlalchemy_integration=True, ) return app celery_app: Celery = Proxy(get_celery_application)
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import __all__ = ['universal'] import os import glob import cameo from cameo.io import load_model from cameo import util from functools import partial from lazy_object_proxy import Proxy class ModelDB(object): pass universal = ModelDB() for file_path in glob.glob( os.path.join(os.path.dirname(cameo.__file__), 'models', 'universal_models', '*.json')): model_id = os.path.splitext(os.path.basename(file_path))[0] setattr(universal, util.str_to_valid_variable_name(model_id), Proxy(partial(load_model, file_path)))
app.config["SERVER_NAME"] = s.server_name app.config["PREFERRED_URL_SCHEME"] = "https" if getenv("DEPLOYMENT") in ("dev", "stg"): app.config["DEBUG"] = True app.logger.setLevel(logging.DEBUG) logger = logging.getLogger("packit_service") logger.info( f"server name = {s.server_name}, all HTTP requests need to use this URL!" ) log_service_versions() # no need to thank me, just buy me a beer logger.debug(f"URL map = {app.url_map}") return app packit_as_a_service = Proxy(get_flask_application) # Make Prometheus Client serve the /metrics endpoint application = DispatcherMiddleware(packit_as_a_service, {"/metrics": prometheus_app()}) # With the code below, you can debug ALL requests coming to flask # @application.before_request # def log_request(): # from flask import request, url_for # import logging # logger = logging.getLogger(__name__) # logger.info("Request Headers %s", request.headers) # logger.info("sample URL: %s", url_for( # "api.doc", # _external=True, # _external = generate a URL with FQDN, not a relative one
def get_project(self, url: str) -> GitProject: return Proxy(partial(self._get_project, url))
k, dist.mean(), dist.std()) _categorical = lambda k, categories: DiscreteHyperParameter( k, categories) def transformHyperDefItemUniversal(self, k, v): return SciPyContinuousHyperParameter(k, v.distribution) def transformResult(self, hyperparamsNative): return super().transformResult( self.__class__.hyperparamsSpecType.native2dict( hyperparamsNative, self)) return PySHACGridSpec PySHACGridSpec = Proxy(loadPyShacGridSpec) class PySHAC(GenericOptimizer): specType = PySHACGridSpec def __init__(self, blackBoxFunc: typing.Callable, spaceSpec: typing.Mapping[str, object], iters: int = 1000, jobs: int = 3, pointsStorage: PointsStorage = None, batches=None, skipCV: bool = False, earlyStop: bool = False, relaxChecks: bool = False,
__all__ = [ "cdmsobj", "axis", "coord", "grid", "hgrid", "avariable", "sliceut", "error", "variable", "fvariable", "tvariable", "dataset", "database", "cache", "selectors", "MV2", "convention", "bindex", "auxcoord", "gengrid", "gsHost", "gsStaticVariable", "gsTimeVariable", "mvBaseWriter", "mvSphereMesh", "mvVsWriter", "mvCdmsRegrid" ] # CDMS datatypes from .cdmsobj import CdArray, CdChar, CdByte, CdDouble, CdFloat, CdFromObject, CdInt, CdLong, CdScalar, CdShort, CdString # noqa from .axis import AbstractAxis # noqa # Functions which operate on all objects or groups of objects # from .cdmsobj import Unlimited, getPathFromTemplate, matchPattern, matchingFiles, searchPattern, searchPredicate, setDebugMode # noqa Unlimited = Proxy(lambda: cdmsobj.Unlimited) getPathFromTemplate = Proxy(lambda: cdmsobj.getPathFromTemplate) matchPattern = Proxy(lambda: cdmsobj.matchPattern) matchingFiles = Proxy(lambda: cdmsobj.matchingFiles) searchPattern = Proxy(lambda: cdmsobj.searchPattern) searchPredicate = Proxy(lambda: cdmsobj.searchPredicate) setDebugMode = Proxy(lambda: cdmsobj.setDebugMode) # Axis functions and classes axisMatches = Proxy(lambda: axis.axisMatches) axisMatchIndex = Proxy(lambda: axis.axisMatchIndex) createAxis = Proxy(lambda: axis.createAxis) createEqualAreaAxis = Proxy(lambda: axis.createEqualAreaAxis) createGaussianAxis = Proxy(lambda: axis.createGaussianAxis) createUniformLatitudeAxis = Proxy(lambda: axis.createUniformLatitudeAxis) createUniformLongitudeAxis = Proxy(lambda: axis.createUniformLongitudeAxis)
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import import sys import os import glob import cameo from cameo.io import load_model from cameo import util from functools import partial from lazy_object_proxy import Proxy __all__ = ['universal'] class ModelDB(object): pass universal = ModelDB() for file_path in glob.glob(os.path.join(os.path.dirname(cameo.__file__), 'models', 'universal_models', '*.json')): model_id = os.path.splitext(os.path.basename(file_path))[0] setattr(universal, util.str_to_valid_variable_name(model_id), Proxy(partial(load_model, file_path))) sys.modules[__name__] = universal
def __init__(self): self._celery_app = None @property def celery_app(self): if self._celery_app is None: redis_host = getenv("REDIS_SERVICE_HOST", "localhost") redis_port = getenv("REDIS_SERVICE_PORT", "6379") redis_db = getenv("REDIS_SERVICE_DB", "0") redis_url = "redis://{host}:{port}/{db}".format(host=redis_host, port=redis_port, db=redis_db) # http://docs.celeryproject.org/en/latest/reference/celery.html#celery.Celery self._celery_app = Celery(backend=redis_url, broker=redis_url) return self._celery_app def get_celery_application(): celerizer = Celerizer() app = celerizer.celery_app configure_sentry( runner_type="packit-worker", celery_integration=True, sqlalchemy_integration=True, ) return app celery_app = Proxy(get_celery_application)
s = ServiceConfig.get_service_config() # https://flask.palletsprojects.com/en/1.1.x/config/#SERVER_NAME # also needs to contain port if it's not 443 app.config["SERVER_NAME"] = s.server_name app.config["PREFERRED_URL_SCHEME"] = "https" if getenv("DEPLOYMENT") in ("dev", "stg"): app.config["DEBUG"] = True app.logger.setLevel(logging.DEBUG) logger = logging.getLogger("packit_service") logger.info( f"server name = {s.server_name}, all HTTP requests need to use this URL!" ) # no need to thank me, just buy me a beer logger.debug(f"URL map = {app.url_map}") return app application = Proxy(get_flask_application) # With the code below, you can debug ALL requests coming to flask # @application.before_request # def log_request(): # from flask import request, url_for # import logging # logger = logging.getLogger(__name__) # logger.info("Request Headers %s", request.headers) # logger.info("sample URL: %s", url_for( # "api.doc", # _external=True, # _external = generate a URL with FQDN, not a relative one # ))
from functools import partial import board import digitalio import adafruit_ssd1306 from lazy_object_proxy import Proxy from rpi_TM1638 import TMBoards from gpiozero import Button # Use lazy object proxy so we can import this project as a library # without affecting the display and such. This allows us to more # easily test the libary and to provide fake clients and servers for testing. led_key = Proxy(partial(TMBoards, dio=18, clk=15, stb=14, brightness=1)) button = Proxy(partial(Button, 23)) def oled_factory(): return adafruit_ssd1306.SSD1306_SPI(128, 64, spi=board.SPI(), dc=digitalio.DigitalInOut(board.D7), reset=digitalio.DigitalInOut( board.D25), cs=digitalio.DigitalInOut(board.D8)) oled = Proxy(oled_factory)
def __new__(cls, *args, **kwargs): orig_cls = type(cls.__name__, (cls,), {}) orig_cls.__new__ = lambda cl_, *args, **kwargs: object.__new__(cl_) return Proxy(lambda: orig_cls(*args, **kwargs))