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
train
verify_callable_argspec
Checks the callable_ to make sure that it satisfies the given expectations. expected_args should be an iterable of Arguments in the order you expect to receive them. expect_starargs means that the function should or should not take a *args param. expect_kwargs says the callable should or should not take **kwargs param. If expected_args, expect_starargs, or expect_kwargs is Argument.ignore, then the checks related to that argument will not occur. Example usage: callable_check( f, [Argument('a'), Argument('b', 1)], expect_starargs=True, expect_kwargs=Argument.ignore )
zipline/utils/argcheck.py
def verify_callable_argspec(callable_, expected_args=Argument.ignore, expect_starargs=Argument.ignore, expect_kwargs=Argument.ignore): """ Checks the callable_ to make sure that it satisfies the given expectations. expected_args should be an iterable of Arguments in the order you expect to receive them. expect_starargs means that the function should or should not take a *args param. expect_kwargs says the callable should or should not take **kwargs param. If expected_args, expect_starargs, or expect_kwargs is Argument.ignore, then the checks related to that argument will not occur. Example usage: callable_check( f, [Argument('a'), Argument('b', 1)], expect_starargs=True, expect_kwargs=Argument.ignore ) """ if not callable(callable_): raise NotCallable(callable_) expected_arg_list = list( expected_args if expected_args is not Argument.ignore else [] ) args, starargs, kwargs = Argument.parse_argspec(callable_) exc_args = callable_, args, starargs, kwargs # Check the *args. _expect_extra( expect_starargs, starargs, UnexpectedStarargs, NoStarargs, exc_args, ) # Check the **kwargs. _expect_extra( expect_kwargs, kwargs, UnexpectedKwargs, NoKwargs, exc_args, ) if expected_args is Argument.ignore: # Ignore the argument list checks. return if len(args) < len(expected_arg_list): # One or more argument that we expected was not present. raise NotEnoughArguments( callable_, args, starargs, kwargs, [arg for arg in expected_arg_list if arg not in args], ) elif len(args) > len(expected_arg_list): raise TooManyArguments( callable_, args, starargs, kwargs ) # Empty argument that will not match with any actual arguments. missing_arg = Argument(object(), object()) for expected, provided in zip_longest(expected_arg_list, args, fillvalue=missing_arg): if not expected.matches(provided): raise MismatchedArguments( callable_, args, starargs, kwargs )
def verify_callable_argspec(callable_, expected_args=Argument.ignore, expect_starargs=Argument.ignore, expect_kwargs=Argument.ignore): """ Checks the callable_ to make sure that it satisfies the given expectations. expected_args should be an iterable of Arguments in the order you expect to receive them. expect_starargs means that the function should or should not take a *args param. expect_kwargs says the callable should or should not take **kwargs param. If expected_args, expect_starargs, or expect_kwargs is Argument.ignore, then the checks related to that argument will not occur. Example usage: callable_check( f, [Argument('a'), Argument('b', 1)], expect_starargs=True, expect_kwargs=Argument.ignore ) """ if not callable(callable_): raise NotCallable(callable_) expected_arg_list = list( expected_args if expected_args is not Argument.ignore else [] ) args, starargs, kwargs = Argument.parse_argspec(callable_) exc_args = callable_, args, starargs, kwargs # Check the *args. _expect_extra( expect_starargs, starargs, UnexpectedStarargs, NoStarargs, exc_args, ) # Check the **kwargs. _expect_extra( expect_kwargs, kwargs, UnexpectedKwargs, NoKwargs, exc_args, ) if expected_args is Argument.ignore: # Ignore the argument list checks. return if len(args) < len(expected_arg_list): # One or more argument that we expected was not present. raise NotEnoughArguments( callable_, args, starargs, kwargs, [arg for arg in expected_arg_list if arg not in args], ) elif len(args) > len(expected_arg_list): raise TooManyArguments( callable_, args, starargs, kwargs ) # Empty argument that will not match with any actual arguments. missing_arg = Argument(object(), object()) for expected, provided in zip_longest(expected_arg_list, args, fillvalue=missing_arg): if not expected.matches(provided): raise MismatchedArguments( callable_, args, starargs, kwargs )
[ "Checks", "the", "callable_", "to", "make", "sure", "that", "it", "satisfies", "the", "given", "expectations", ".", "expected_args", "should", "be", "an", "iterable", "of", "Arguments", "in", "the", "order", "you", "expect", "to", "receive", "them", ".", "ex...
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/argcheck.py#L143-L222
[ "def", "verify_callable_argspec", "(", "callable_", ",", "expected_args", "=", "Argument", ".", "ignore", ",", "expect_starargs", "=", "Argument", ".", "ignore", ",", "expect_kwargs", "=", "Argument", ".", "ignore", ")", ":", "if", "not", "callable", "(", "cal...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Argument.parse_argspec
Takes a callable and returns a tuple with the list of Argument objects, the name of *args, and the name of **kwargs. If *args or **kwargs is not present, it will be None. This returns a namedtuple called Argspec that has three fields named: args, starargs, and kwargs.
zipline/utils/argcheck.py
def parse_argspec(callable_): """ Takes a callable and returns a tuple with the list of Argument objects, the name of *args, and the name of **kwargs. If *args or **kwargs is not present, it will be None. This returns a namedtuple called Argspec that has three fields named: args, starargs, and kwargs. """ args, varargs, keywords, defaults = getargspec(callable_) defaults = list(defaults or []) if getattr(callable_, '__self__', None) is not None: # This is a bound method, drop the self param. args = args[1:] first_default = len(args) - len(defaults) return Argspec( [Argument(arg, Argument.no_default if n < first_default else defaults[n - first_default]) for n, arg in enumerate(args)], varargs, keywords, )
def parse_argspec(callable_): """ Takes a callable and returns a tuple with the list of Argument objects, the name of *args, and the name of **kwargs. If *args or **kwargs is not present, it will be None. This returns a namedtuple called Argspec that has three fields named: args, starargs, and kwargs. """ args, varargs, keywords, defaults = getargspec(callable_) defaults = list(defaults or []) if getattr(callable_, '__self__', None) is not None: # This is a bound method, drop the self param. args = args[1:] first_default = len(args) - len(defaults) return Argspec( [Argument(arg, Argument.no_default if n < first_default else defaults[n - first_default]) for n, arg in enumerate(args)], varargs, keywords, )
[ "Takes", "a", "callable", "and", "returns", "a", "tuple", "with", "the", "list", "of", "Argument", "objects", "the", "name", "of", "*", "args", "and", "the", "name", "of", "**", "kwargs", ".", "If", "*", "args", "or", "**", "kwargs", "is", "not", "pr...
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/utils/argcheck.py#L98-L120
[ "def", "parse_argspec", "(", "callable_", ")", ":", "args", ",", "varargs", ",", "keywords", ",", "defaults", "=", "getargspec", "(", "callable_", ")", "defaults", "=", "list", "(", "defaults", "or", "[", "]", ")", "if", "getattr", "(", "callable_", ",",...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
StaticRestrictions.is_restricted
An asset is restricted for all dts if it is in the static list.
zipline/finance/asset_restrictions.py
def is_restricted(self, assets, dt): """ An asset is restricted for all dts if it is in the static list. """ if isinstance(assets, Asset): return assets in self._restricted_set return pd.Series( index=pd.Index(assets), data=vectorized_is_element(assets, self._restricted_set) )
def is_restricted(self, assets, dt): """ An asset is restricted for all dts if it is in the static list. """ if isinstance(assets, Asset): return assets in self._restricted_set return pd.Series( index=pd.Index(assets), data=vectorized_is_element(assets, self._restricted_set) )
[ "An", "asset", "is", "restricted", "for", "all", "dts", "if", "it", "is", "in", "the", "static", "list", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/asset_restrictions.py#L143-L152
[ "def", "is_restricted", "(", "self", ",", "assets", ",", "dt", ")", ":", "if", "isinstance", "(", "assets", ",", "Asset", ")", ":", "return", "assets", "in", "self", ".", "_restricted_set", "return", "pd", ".", "Series", "(", "index", "=", "pd", ".", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
HistoricalRestrictions.is_restricted
Returns whether or not an asset or iterable of assets is restricted on a dt.
zipline/finance/asset_restrictions.py
def is_restricted(self, assets, dt): """ Returns whether or not an asset or iterable of assets is restricted on a dt. """ if isinstance(assets, Asset): return self._is_restricted_for_asset(assets, dt) is_restricted = partial(self._is_restricted_for_asset, dt=dt) return pd.Series( index=pd.Index(assets), data=vectorize(is_restricted, otypes=[bool])(assets) )
def is_restricted(self, assets, dt): """ Returns whether or not an asset or iterable of assets is restricted on a dt. """ if isinstance(assets, Asset): return self._is_restricted_for_asset(assets, dt) is_restricted = partial(self._is_restricted_for_asset, dt=dt) return pd.Series( index=pd.Index(assets), data=vectorize(is_restricted, otypes=[bool])(assets) )
[ "Returns", "whether", "or", "not", "an", "asset", "or", "iterable", "of", "assets", "is", "restricted", "on", "a", "dt", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/asset_restrictions.py#L177-L189
[ "def", "is_restricted", "(", "self", ",", "assets", ",", "dt", ")", ":", "if", "isinstance", "(", "assets", ",", "Asset", ")", ":", "return", "self", ".", "_is_restricted_for_asset", "(", "assets", ",", "dt", ")", "is_restricted", "=", "partial", "(", "s...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
PositionTracker.handle_splits
Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list A list of splits. Each split is a tuple of (asset, ratio). Returns ------- int: The leftover cash from fractional shares after modifying each position.
zipline/finance/ledger.py
def handle_splits(self, splits): """Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list A list of splits. Each split is a tuple of (asset, ratio). Returns ------- int: The leftover cash from fractional shares after modifying each position. """ total_leftover_cash = 0 for asset, ratio in splits: if asset in self.positions: self._dirty_stats = True # Make the position object handle the split. It returns the # leftover cash from a fractional share, if there is any. position = self.positions[asset] leftover_cash = position.handle_split(asset, ratio) total_leftover_cash += leftover_cash return total_leftover_cash
def handle_splits(self, splits): """Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list A list of splits. Each split is a tuple of (asset, ratio). Returns ------- int: The leftover cash from fractional shares after modifying each position. """ total_leftover_cash = 0 for asset, ratio in splits: if asset in self.positions: self._dirty_stats = True # Make the position object handle the split. It returns the # leftover cash from a fractional share, if there is any. position = self.positions[asset] leftover_cash = position.handle_split(asset, ratio) total_leftover_cash += leftover_cash return total_leftover_cash
[ "Processes", "a", "list", "of", "splits", "by", "modifying", "any", "positions", "as", "needed", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L114-L139
[ "def", "handle_splits", "(", "self", ",", "splits", ")", ":", "total_leftover_cash", "=", "0", "for", "asset", ",", "ratio", "in", "splits", ":", "if", "asset", "in", "self", ".", "positions", ":", "self", ".", "_dirty_stats", "=", "True", "# Make the posi...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
PositionTracker.earn_dividends
Given a list of dividends whose ex_dates are all the next trading day, calculate and store the cash and/or stock payments to be paid on each dividend's pay date. Parameters ---------- cash_dividends : iterable of (asset, amount, pay_date) namedtuples stock_dividends: iterable of (asset, payment_asset, ratio, pay_date) namedtuples.
zipline/finance/ledger.py
def earn_dividends(self, cash_dividends, stock_dividends): """Given a list of dividends whose ex_dates are all the next trading day, calculate and store the cash and/or stock payments to be paid on each dividend's pay date. Parameters ---------- cash_dividends : iterable of (asset, amount, pay_date) namedtuples stock_dividends: iterable of (asset, payment_asset, ratio, pay_date) namedtuples. """ for cash_dividend in cash_dividends: self._dirty_stats = True # only mark dirty if we pay a dividend # Store the earned dividends so that they can be paid on the # dividends' pay_dates. div_owed = self.positions[cash_dividend.asset].earn_dividend( cash_dividend, ) try: self._unpaid_dividends[cash_dividend.pay_date].append(div_owed) except KeyError: self._unpaid_dividends[cash_dividend.pay_date] = [div_owed] for stock_dividend in stock_dividends: self._dirty_stats = True # only mark dirty if we pay a dividend div_owed = self.positions[ stock_dividend.asset ].earn_stock_dividend(stock_dividend) try: self._unpaid_stock_dividends[stock_dividend.pay_date].append( div_owed, ) except KeyError: self._unpaid_stock_dividends[stock_dividend.pay_date] = [ div_owed, ]
def earn_dividends(self, cash_dividends, stock_dividends): """Given a list of dividends whose ex_dates are all the next trading day, calculate and store the cash and/or stock payments to be paid on each dividend's pay date. Parameters ---------- cash_dividends : iterable of (asset, amount, pay_date) namedtuples stock_dividends: iterable of (asset, payment_asset, ratio, pay_date) namedtuples. """ for cash_dividend in cash_dividends: self._dirty_stats = True # only mark dirty if we pay a dividend # Store the earned dividends so that they can be paid on the # dividends' pay_dates. div_owed = self.positions[cash_dividend.asset].earn_dividend( cash_dividend, ) try: self._unpaid_dividends[cash_dividend.pay_date].append(div_owed) except KeyError: self._unpaid_dividends[cash_dividend.pay_date] = [div_owed] for stock_dividend in stock_dividends: self._dirty_stats = True # only mark dirty if we pay a dividend div_owed = self.positions[ stock_dividend.asset ].earn_stock_dividend(stock_dividend) try: self._unpaid_stock_dividends[stock_dividend.pay_date].append( div_owed, ) except KeyError: self._unpaid_stock_dividends[stock_dividend.pay_date] = [ div_owed, ]
[ "Given", "a", "list", "of", "dividends", "whose", "ex_dates", "are", "all", "the", "next", "trading", "day", "calculate", "and", "store", "the", "cash", "and", "/", "or", "stock", "payments", "to", "be", "paid", "on", "each", "dividend", "s", "pay", "dat...
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L141-L179
[ "def", "earn_dividends", "(", "self", ",", "cash_dividends", ",", "stock_dividends", ")", ":", "for", "cash_dividend", "in", "cash_dividends", ":", "self", ".", "_dirty_stats", "=", "True", "# only mark dirty if we pay a dividend", "# Store the earned dividends so that they...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
PositionTracker.pay_dividends
Returns a cash payment based on the dividends that should be paid out according to the accumulated bookkeeping of earned, unpaid, and stock dividends.
zipline/finance/ledger.py
def pay_dividends(self, next_trading_day): """ Returns a cash payment based on the dividends that should be paid out according to the accumulated bookkeeping of earned, unpaid, and stock dividends. """ net_cash_payment = 0.0 try: payments = self._unpaid_dividends[next_trading_day] # Mark these dividends as paid by dropping them from our unpaid del self._unpaid_dividends[next_trading_day] except KeyError: payments = [] # representing the fact that we're required to reimburse the owner of # the stock for any dividends paid while borrowing. for payment in payments: net_cash_payment += payment['amount'] # Add stock for any stock dividends paid. Again, the values here may # be negative in the case of short positions. try: stock_payments = self._unpaid_stock_dividends[next_trading_day] except KeyError: stock_payments = [] for stock_payment in stock_payments: payment_asset = stock_payment['payment_asset'] share_count = stock_payment['share_count'] # note we create a Position for stock dividend if we don't # already own the asset if payment_asset in self.positions: position = self.positions[payment_asset] else: position = self.positions[payment_asset] = Position( payment_asset, ) position.amount += share_count return net_cash_payment
def pay_dividends(self, next_trading_day): """ Returns a cash payment based on the dividends that should be paid out according to the accumulated bookkeeping of earned, unpaid, and stock dividends. """ net_cash_payment = 0.0 try: payments = self._unpaid_dividends[next_trading_day] # Mark these dividends as paid by dropping them from our unpaid del self._unpaid_dividends[next_trading_day] except KeyError: payments = [] # representing the fact that we're required to reimburse the owner of # the stock for any dividends paid while borrowing. for payment in payments: net_cash_payment += payment['amount'] # Add stock for any stock dividends paid. Again, the values here may # be negative in the case of short positions. try: stock_payments = self._unpaid_stock_dividends[next_trading_day] except KeyError: stock_payments = [] for stock_payment in stock_payments: payment_asset = stock_payment['payment_asset'] share_count = stock_payment['share_count'] # note we create a Position for stock dividend if we don't # already own the asset if payment_asset in self.positions: position = self.positions[payment_asset] else: position = self.positions[payment_asset] = Position( payment_asset, ) position.amount += share_count return net_cash_payment
[ "Returns", "a", "cash", "payment", "based", "on", "the", "dividends", "that", "should", "be", "paid", "out", "according", "to", "the", "accumulated", "bookkeeping", "of", "earned", "unpaid", "and", "stock", "dividends", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L181-L222
[ "def", "pay_dividends", "(", "self", ",", "next_trading_day", ")", ":", "net_cash_payment", "=", "0.0", "try", ":", "payments", "=", "self", ".", "_unpaid_dividends", "[", "next_trading_day", "]", "# Mark these dividends as paid by dropping them from our unpaid", "del", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
PositionTracker.stats
The current status of the positions. Returns ------- stats : PositionStats The current stats position stats. Notes ----- This is cached, repeated access will not recompute the stats until the stats may have changed.
zipline/finance/ledger.py
def stats(self): """The current status of the positions. Returns ------- stats : PositionStats The current stats position stats. Notes ----- This is cached, repeated access will not recompute the stats until the stats may have changed. """ if self._dirty_stats: calculate_position_tracker_stats(self.positions, self._stats) self._dirty_stats = False return self._stats
def stats(self): """The current status of the positions. Returns ------- stats : PositionStats The current stats position stats. Notes ----- This is cached, repeated access will not recompute the stats until the stats may have changed. """ if self._dirty_stats: calculate_position_tracker_stats(self.positions, self._stats) self._dirty_stats = False return self._stats
[ "The", "current", "status", "of", "the", "positions", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L288-L305
[ "def", "stats", "(", "self", ")", ":", "if", "self", ".", "_dirty_stats", ":", "calculate_position_tracker_stats", "(", "self", ".", "positions", ",", "self", ".", "_stats", ")", "self", ".", "_dirty_stats", "=", "False", "return", "self", ".", "_stats" ]
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Ledger.process_transaction
Add a transaction to ledger, updating the current state as needed. Parameters ---------- transaction : zp.Transaction The transaction to execute.
zipline/finance/ledger.py
def process_transaction(self, transaction): """Add a transaction to ledger, updating the current state as needed. Parameters ---------- transaction : zp.Transaction The transaction to execute. """ asset = transaction.asset if isinstance(asset, Future): try: old_price = self._payout_last_sale_prices[asset] except KeyError: self._payout_last_sale_prices[asset] = transaction.price else: position = self.position_tracker.positions[asset] amount = position.amount price = transaction.price self._cash_flow( self._calculate_payout( asset.price_multiplier, amount, old_price, price, ), ) if amount + transaction.amount == 0: del self._payout_last_sale_prices[asset] else: self._payout_last_sale_prices[asset] = price else: self._cash_flow(-(transaction.price * transaction.amount)) self.position_tracker.execute_transaction(transaction) # we only ever want the dict form from now on transaction_dict = transaction.to_dict() try: self._processed_transactions[transaction.dt].append( transaction_dict, ) except KeyError: self._processed_transactions[transaction.dt] = [transaction_dict]
def process_transaction(self, transaction): """Add a transaction to ledger, updating the current state as needed. Parameters ---------- transaction : zp.Transaction The transaction to execute. """ asset = transaction.asset if isinstance(asset, Future): try: old_price = self._payout_last_sale_prices[asset] except KeyError: self._payout_last_sale_prices[asset] = transaction.price else: position = self.position_tracker.positions[asset] amount = position.amount price = transaction.price self._cash_flow( self._calculate_payout( asset.price_multiplier, amount, old_price, price, ), ) if amount + transaction.amount == 0: del self._payout_last_sale_prices[asset] else: self._payout_last_sale_prices[asset] = price else: self._cash_flow(-(transaction.price * transaction.amount)) self.position_tracker.execute_transaction(transaction) # we only ever want the dict form from now on transaction_dict = transaction.to_dict() try: self._processed_transactions[transaction.dt].append( transaction_dict, ) except KeyError: self._processed_transactions[transaction.dt] = [transaction_dict]
[ "Add", "a", "transaction", "to", "ledger", "updating", "the", "current", "state", "as", "needed", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L479-L523
[ "def", "process_transaction", "(", "self", ",", "transaction", ")", ":", "asset", "=", "transaction", ".", "asset", "if", "isinstance", "(", "asset", ",", "Future", ")", ":", "try", ":", "old_price", "=", "self", ".", "_payout_last_sale_prices", "[", "asset"...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Ledger.process_splits
Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list[(Asset, float)] A list of splits. Each split is a tuple of (asset, ratio).
zipline/finance/ledger.py
def process_splits(self, splits): """Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list[(Asset, float)] A list of splits. Each split is a tuple of (asset, ratio). """ leftover_cash = self.position_tracker.handle_splits(splits) if leftover_cash > 0: self._cash_flow(leftover_cash)
def process_splits(self, splits): """Processes a list of splits by modifying any positions as needed. Parameters ---------- splits: list[(Asset, float)] A list of splits. Each split is a tuple of (asset, ratio). """ leftover_cash = self.position_tracker.handle_splits(splits) if leftover_cash > 0: self._cash_flow(leftover_cash)
[ "Processes", "a", "list", "of", "splits", "by", "modifying", "any", "positions", "as", "needed", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L525-L535
[ "def", "process_splits", "(", "self", ",", "splits", ")", ":", "leftover_cash", "=", "self", ".", "position_tracker", ".", "handle_splits", "(", "splits", ")", "if", "leftover_cash", ">", "0", ":", "self", ".", "_cash_flow", "(", "leftover_cash", ")" ]
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Ledger.process_order
Keep track of an order that was placed. Parameters ---------- order : zp.Order The order to record.
zipline/finance/ledger.py
def process_order(self, order): """Keep track of an order that was placed. Parameters ---------- order : zp.Order The order to record. """ try: dt_orders = self._orders_by_modified[order.dt] except KeyError: self._orders_by_modified[order.dt] = OrderedDict([ (order.id, order), ]) self._orders_by_id[order.id] = order else: self._orders_by_id[order.id] = dt_orders[order.id] = order # to preserve the order of the orders by modified date move_to_end(dt_orders, order.id, last=True) move_to_end(self._orders_by_id, order.id, last=True)
def process_order(self, order): """Keep track of an order that was placed. Parameters ---------- order : zp.Order The order to record. """ try: dt_orders = self._orders_by_modified[order.dt] except KeyError: self._orders_by_modified[order.dt] = OrderedDict([ (order.id, order), ]) self._orders_by_id[order.id] = order else: self._orders_by_id[order.id] = dt_orders[order.id] = order # to preserve the order of the orders by modified date move_to_end(dt_orders, order.id, last=True) move_to_end(self._orders_by_id, order.id, last=True)
[ "Keep", "track", "of", "an", "order", "that", "was", "placed", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L537-L557
[ "def", "process_order", "(", "self", ",", "order", ")", ":", "try", ":", "dt_orders", "=", "self", ".", "_orders_by_modified", "[", "order", ".", "dt", "]", "except", "KeyError", ":", "self", ".", "_orders_by_modified", "[", "order", ".", "dt", "]", "=",...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Ledger.process_commission
Process the commission. Parameters ---------- commission : zp.Event The commission being paid.
zipline/finance/ledger.py
def process_commission(self, commission): """Process the commission. Parameters ---------- commission : zp.Event The commission being paid. """ asset = commission['asset'] cost = commission['cost'] self.position_tracker.handle_commission(asset, cost) self._cash_flow(-cost)
def process_commission(self, commission): """Process the commission. Parameters ---------- commission : zp.Event The commission being paid. """ asset = commission['asset'] cost = commission['cost'] self.position_tracker.handle_commission(asset, cost) self._cash_flow(-cost)
[ "Process", "the", "commission", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L559-L571
[ "def", "process_commission", "(", "self", ",", "commission", ")", ":", "asset", "=", "commission", "[", "'asset'", "]", "cost", "=", "commission", "[", "'cost'", "]", "self", ".", "position_tracker", ".", "handle_commission", "(", "asset", ",", "cost", ")", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Ledger.process_dividends
Process dividends for the next session. This will earn us any dividends whose ex-date is the next session as well as paying out any dividends whose pay-date is the next session
zipline/finance/ledger.py
def process_dividends(self, next_session, asset_finder, adjustment_reader): """Process dividends for the next session. This will earn us any dividends whose ex-date is the next session as well as paying out any dividends whose pay-date is the next session """ position_tracker = self.position_tracker # Earn dividends whose ex_date is the next trading day. We need to # check if we own any of these stocks so we know to pay them out when # the pay date comes. held_sids = set(position_tracker.positions) if held_sids: cash_dividends = adjustment_reader.get_dividends_with_ex_date( held_sids, next_session, asset_finder ) stock_dividends = ( adjustment_reader.get_stock_dividends_with_ex_date( held_sids, next_session, asset_finder ) ) # Earning a dividend just marks that we need to get paid out on # the dividend's pay-date. This does not affect our cash yet. position_tracker.earn_dividends( cash_dividends, stock_dividends, ) # Pay out the dividends whose pay-date is the next session. This does # affect out cash. self._cash_flow( position_tracker.pay_dividends( next_session, ), )
def process_dividends(self, next_session, asset_finder, adjustment_reader): """Process dividends for the next session. This will earn us any dividends whose ex-date is the next session as well as paying out any dividends whose pay-date is the next session """ position_tracker = self.position_tracker # Earn dividends whose ex_date is the next trading day. We need to # check if we own any of these stocks so we know to pay them out when # the pay date comes. held_sids = set(position_tracker.positions) if held_sids: cash_dividends = adjustment_reader.get_dividends_with_ex_date( held_sids, next_session, asset_finder ) stock_dividends = ( adjustment_reader.get_stock_dividends_with_ex_date( held_sids, next_session, asset_finder ) ) # Earning a dividend just marks that we need to get paid out on # the dividend's pay-date. This does not affect our cash yet. position_tracker.earn_dividends( cash_dividends, stock_dividends, ) # Pay out the dividends whose pay-date is the next session. This does # affect out cash. self._cash_flow( position_tracker.pay_dividends( next_session, ), )
[ "Process", "dividends", "for", "the", "next", "session", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L582-L621
[ "def", "process_dividends", "(", "self", ",", "next_session", ",", "asset_finder", ",", "adjustment_reader", ")", ":", "position_tracker", "=", "self", ".", "position_tracker", "# Earn dividends whose ex_date is the next trading day. We need to", "# check if we own any of these s...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Ledger.transactions
Retrieve the dict-form of all of the transactions in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up transactions for. If not passed, or None is explicitly passed, all of the transactions will be returned. Returns ------- transactions : list[dict] The transaction information.
zipline/finance/ledger.py
def transactions(self, dt=None): """Retrieve the dict-form of all of the transactions in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up transactions for. If not passed, or None is explicitly passed, all of the transactions will be returned. Returns ------- transactions : list[dict] The transaction information. """ if dt is None: # flatten the by-day transactions return [ txn for by_day in itervalues(self._processed_transactions) for txn in by_day ] return self._processed_transactions.get(dt, [])
def transactions(self, dt=None): """Retrieve the dict-form of all of the transactions in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up transactions for. If not passed, or None is explicitly passed, all of the transactions will be returned. Returns ------- transactions : list[dict] The transaction information. """ if dt is None: # flatten the by-day transactions return [ txn for by_day in itervalues(self._processed_transactions) for txn in by_day ] return self._processed_transactions.get(dt, [])
[ "Retrieve", "the", "dict", "-", "form", "of", "all", "of", "the", "transactions", "in", "a", "given", "bar", "or", "for", "the", "whole", "simulation", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L631-L655
[ "def", "transactions", "(", "self", ",", "dt", "=", "None", ")", ":", "if", "dt", "is", "None", ":", "# flatten the by-day transactions", "return", "[", "txn", "for", "by_day", "in", "itervalues", "(", "self", ".", "_processed_transactions", ")", "for", "txn...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Ledger.orders
Retrieve the dict-form of all of the orders in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up order for. If not passed, or None is explicitly passed, all of the orders will be returned. Returns ------- orders : list[dict] The order information.
zipline/finance/ledger.py
def orders(self, dt=None): """Retrieve the dict-form of all of the orders in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up order for. If not passed, or None is explicitly passed, all of the orders will be returned. Returns ------- orders : list[dict] The order information. """ if dt is None: # orders by id is already flattened return [o.to_dict() for o in itervalues(self._orders_by_id)] return [ o.to_dict() for o in itervalues(self._orders_by_modified.get(dt, {})) ]
def orders(self, dt=None): """Retrieve the dict-form of all of the orders in a given bar or for the whole simulation. Parameters ---------- dt : pd.Timestamp or None, optional The particular datetime to look up order for. If not passed, or None is explicitly passed, all of the orders will be returned. Returns ------- orders : list[dict] The order information. """ if dt is None: # orders by id is already flattened return [o.to_dict() for o in itervalues(self._orders_by_id)] return [ o.to_dict() for o in itervalues(self._orders_by_modified.get(dt, {})) ]
[ "Retrieve", "the", "dict", "-", "form", "of", "all", "of", "the", "orders", "in", "a", "given", "bar", "or", "for", "the", "whole", "simulation", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L657-L679
[ "def", "orders", "(", "self", ",", "dt", "=", "None", ")", ":", "if", "dt", "is", "None", ":", "# orders by id is already flattened", "return", "[", "o", ".", "to_dict", "(", ")", "for", "o", "in", "itervalues", "(", "self", ".", "_orders_by_id", ")", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Ledger.update_portfolio
Force a computation of the current portfolio state.
zipline/finance/ledger.py
def update_portfolio(self): """Force a computation of the current portfolio state. """ if not self._dirty_portfolio: return portfolio = self._portfolio pt = self.position_tracker portfolio.positions = pt.get_positions() position_stats = pt.stats portfolio.positions_value = position_value = ( position_stats.net_value ) portfolio.positions_exposure = position_stats.net_exposure self._cash_flow(self._get_payout_total(pt.positions)) start_value = portfolio.portfolio_value # update the new starting value portfolio.portfolio_value = end_value = portfolio.cash + position_value pnl = end_value - start_value if start_value != 0: returns = pnl / start_value else: returns = 0.0 portfolio.pnl += pnl portfolio.returns = ( (1 + portfolio.returns) * (1 + returns) - 1 ) # the portfolio has been fully synced self._dirty_portfolio = False
def update_portfolio(self): """Force a computation of the current portfolio state. """ if not self._dirty_portfolio: return portfolio = self._portfolio pt = self.position_tracker portfolio.positions = pt.get_positions() position_stats = pt.stats portfolio.positions_value = position_value = ( position_stats.net_value ) portfolio.positions_exposure = position_stats.net_exposure self._cash_flow(self._get_payout_total(pt.positions)) start_value = portfolio.portfolio_value # update the new starting value portfolio.portfolio_value = end_value = portfolio.cash + position_value pnl = end_value - start_value if start_value != 0: returns = pnl / start_value else: returns = 0.0 portfolio.pnl += pnl portfolio.returns = ( (1 + portfolio.returns) * (1 + returns) - 1 ) # the portfolio has been fully synced self._dirty_portfolio = False
[ "Force", "a", "computation", "of", "the", "current", "portfolio", "state", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L703-L740
[ "def", "update_portfolio", "(", "self", ")", ":", "if", "not", "self", ".", "_dirty_portfolio", ":", "return", "portfolio", "=", "self", ".", "_portfolio", "pt", "=", "self", ".", "position_tracker", "portfolio", ".", "positions", "=", "pt", ".", "get_positi...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Ledger.override_account_fields
Override fields on ``self.account``.
zipline/finance/ledger.py
def override_account_fields(self, settled_cash=not_overridden, accrued_interest=not_overridden, buying_power=not_overridden, equity_with_loan=not_overridden, total_positions_value=not_overridden, total_positions_exposure=not_overridden, regt_equity=not_overridden, regt_margin=not_overridden, initial_margin_requirement=not_overridden, maintenance_margin_requirement=not_overridden, available_funds=not_overridden, excess_liquidity=not_overridden, cushion=not_overridden, day_trades_remaining=not_overridden, leverage=not_overridden, net_leverage=not_overridden, net_liquidation=not_overridden): """Override fields on ``self.account``. """ # mark that the portfolio is dirty to override the fields again self._dirty_account = True self._account_overrides = kwargs = { k: v for k, v in locals().items() if v is not not_overridden } del kwargs['self']
def override_account_fields(self, settled_cash=not_overridden, accrued_interest=not_overridden, buying_power=not_overridden, equity_with_loan=not_overridden, total_positions_value=not_overridden, total_positions_exposure=not_overridden, regt_equity=not_overridden, regt_margin=not_overridden, initial_margin_requirement=not_overridden, maintenance_margin_requirement=not_overridden, available_funds=not_overridden, excess_liquidity=not_overridden, cushion=not_overridden, day_trades_remaining=not_overridden, leverage=not_overridden, net_leverage=not_overridden, net_liquidation=not_overridden): """Override fields on ``self.account``. """ # mark that the portfolio is dirty to override the fields again self._dirty_account = True self._account_overrides = kwargs = { k: v for k, v in locals().items() if v is not not_overridden } del kwargs['self']
[ "Override", "fields", "on", "self", ".", "account", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/finance/ledger.py#L766-L791
[ "def", "override_account_fields", "(", "self", ",", "settled_cash", "=", "not_overridden", ",", "accrued_interest", "=", "not_overridden", ",", "buying_power", "=", "not_overridden", ",", "equity_with_loan", "=", "not_overridden", ",", "total_positions_value", "=", "not...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
datashape_type_to_numpy
Given a datashape type, return the associated numpy type. Maps datashape's DateTime type to numpy's `datetime64[ns]` dtype, since the numpy datetime returned by datashape isn't supported by pipeline. Parameters ---------- type_: datashape.coretypes.Type The datashape type. Returns ------- type_ np.dtype The numpy dtype.
zipline/pipeline/loaders/blaze/core.py
def datashape_type_to_numpy(type_): """ Given a datashape type, return the associated numpy type. Maps datashape's DateTime type to numpy's `datetime64[ns]` dtype, since the numpy datetime returned by datashape isn't supported by pipeline. Parameters ---------- type_: datashape.coretypes.Type The datashape type. Returns ------- type_ np.dtype The numpy dtype. """ if isinstance(type_, Option): type_ = type_.ty if isinstance(type_, DateTime): return np.dtype('datetime64[ns]') if isinstance(type_, String): return np.dtype(object) if type_ in integral: return np.dtype('int64') else: return type_.to_numpy_dtype()
def datashape_type_to_numpy(type_): """ Given a datashape type, return the associated numpy type. Maps datashape's DateTime type to numpy's `datetime64[ns]` dtype, since the numpy datetime returned by datashape isn't supported by pipeline. Parameters ---------- type_: datashape.coretypes.Type The datashape type. Returns ------- type_ np.dtype The numpy dtype. """ if isinstance(type_, Option): type_ = type_.ty if isinstance(type_, DateTime): return np.dtype('datetime64[ns]') if isinstance(type_, String): return np.dtype(object) if type_ in integral: return np.dtype('int64') else: return type_.to_numpy_dtype()
[ "Given", "a", "datashape", "type", "return", "the", "associated", "numpy", "type", ".", "Maps", "datashape", "s", "DateTime", "type", "to", "numpy", "s", "datetime64", "[", "ns", "]", "dtype", "since", "the", "numpy", "datetime", "returned", "by", "datashape...
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L246-L272
[ "def", "datashape_type_to_numpy", "(", "type_", ")", ":", "if", "isinstance", "(", "type_", ",", "Option", ")", ":", "type_", "=", "type_", ".", "ty", "if", "isinstance", "(", "type_", ",", "DateTime", ")", ":", "return", "np", ".", "dtype", "(", "'dat...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
new_dataset
Creates or returns a dataset from a blaze expression. Parameters ---------- expr : Expr The blaze expression representing the values. missing_values : frozenset((name, value) pairs Association pairs column name and missing_value for that column. This needs to be a frozenset rather than a dict or tuple of tuples because we want a collection that's unordered but still hashable. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. Returns ------- ds : type A new dataset type. Notes ----- This function is memoized. repeated calls with the same inputs will return the same type.
zipline/pipeline/loaders/blaze/core.py
def new_dataset(expr, missing_values, domain): """ Creates or returns a dataset from a blaze expression. Parameters ---------- expr : Expr The blaze expression representing the values. missing_values : frozenset((name, value) pairs Association pairs column name and missing_value for that column. This needs to be a frozenset rather than a dict or tuple of tuples because we want a collection that's unordered but still hashable. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. Returns ------- ds : type A new dataset type. Notes ----- This function is memoized. repeated calls with the same inputs will return the same type. """ missing_values = dict(missing_values) class_dict = {'ndim': 2 if SID_FIELD_NAME in expr.fields else 1} for name, type_ in expr.dshape.measure.fields: # Don't generate a column for sid or timestamp, since they're # implicitly the labels if the arrays that will be passed to pipeline # Terms. if name in (SID_FIELD_NAME, TS_FIELD_NAME): continue type_ = datashape_type_to_numpy(type_) if can_represent_dtype(type_): col = Column( type_, missing_values.get(name, NotSpecified), ) else: col = NonPipelineField(name, type_) class_dict[name] = col if 'domain' in class_dict: raise ValueError("Got a column named 'domain' in new_dataset(). " "'domain' is reserved.") class_dict['domain'] = domain name = expr._name if name is None: name = next(_new_names) # unicode is a name error in py3 but the branch is only hit # when we are in python 2. if PY2 and isinstance(name, unicode): # pragma: no cover # noqa name = name.encode('utf-8') return type(name, (DataSet,), class_dict)
def new_dataset(expr, missing_values, domain): """ Creates or returns a dataset from a blaze expression. Parameters ---------- expr : Expr The blaze expression representing the values. missing_values : frozenset((name, value) pairs Association pairs column name and missing_value for that column. This needs to be a frozenset rather than a dict or tuple of tuples because we want a collection that's unordered but still hashable. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. Returns ------- ds : type A new dataset type. Notes ----- This function is memoized. repeated calls with the same inputs will return the same type. """ missing_values = dict(missing_values) class_dict = {'ndim': 2 if SID_FIELD_NAME in expr.fields else 1} for name, type_ in expr.dshape.measure.fields: # Don't generate a column for sid or timestamp, since they're # implicitly the labels if the arrays that will be passed to pipeline # Terms. if name in (SID_FIELD_NAME, TS_FIELD_NAME): continue type_ = datashape_type_to_numpy(type_) if can_represent_dtype(type_): col = Column( type_, missing_values.get(name, NotSpecified), ) else: col = NonPipelineField(name, type_) class_dict[name] = col if 'domain' in class_dict: raise ValueError("Got a column named 'domain' in new_dataset(). " "'domain' is reserved.") class_dict['domain'] = domain name = expr._name if name is None: name = next(_new_names) # unicode is a name error in py3 but the branch is only hit # when we are in python 2. if PY2 and isinstance(name, unicode): # pragma: no cover # noqa name = name.encode('utf-8') return type(name, (DataSet,), class_dict)
[ "Creates", "or", "returns", "a", "dataset", "from", "a", "blaze", "expression", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L276-L334
[ "def", "new_dataset", "(", "expr", ",", "missing_values", ",", "domain", ")", ":", "missing_values", "=", "dict", "(", "missing_values", ")", "class_dict", "=", "{", "'ndim'", ":", "2", "if", "SID_FIELD_NAME", "in", "expr", ".", "fields", "else", "1", "}",...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
_check_resources
Validate that the expression and resources passed match up. Parameters ---------- name : str The name of the argument we are checking. expr : Expr The potentially bound expr. resources The explicitly passed resources to compute expr. Raises ------ ValueError If the resources do not match for an expression.
zipline/pipeline/loaders/blaze/core.py
def _check_resources(name, expr, resources): """Validate that the expression and resources passed match up. Parameters ---------- name : str The name of the argument we are checking. expr : Expr The potentially bound expr. resources The explicitly passed resources to compute expr. Raises ------ ValueError If the resources do not match for an expression. """ if expr is None: return bound = expr._resources() if not bound and resources is None: raise ValueError('no resources provided to compute %s' % name) if bound and resources: raise ValueError( 'explicit and implicit resources provided to compute %s' % name, )
def _check_resources(name, expr, resources): """Validate that the expression and resources passed match up. Parameters ---------- name : str The name of the argument we are checking. expr : Expr The potentially bound expr. resources The explicitly passed resources to compute expr. Raises ------ ValueError If the resources do not match for an expression. """ if expr is None: return bound = expr._resources() if not bound and resources is None: raise ValueError('no resources provided to compute %s' % name) if bound and resources: raise ValueError( 'explicit and implicit resources provided to compute %s' % name, )
[ "Validate", "that", "the", "expression", "and", "resources", "passed", "match", "up", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L337-L362
[ "def", "_check_resources", "(", "name", ",", "expr", ",", "resources", ")", ":", "if", "expr", "is", "None", ":", "return", "bound", "=", "expr", ".", "_resources", "(", ")", "if", "not", "bound", "and", "resources", "is", "None", ":", "raise", "ValueE...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
_check_datetime_field
Check that a field is a datetime inside some measure. Parameters ---------- name : str The name of the field to check. measure : Record The record to check the field of. Raises ------ TypeError If the field is not a datetime inside ``measure``.
zipline/pipeline/loaders/blaze/core.py
def _check_datetime_field(name, measure): """Check that a field is a datetime inside some measure. Parameters ---------- name : str The name of the field to check. measure : Record The record to check the field of. Raises ------ TypeError If the field is not a datetime inside ``measure``. """ if not isinstance(measure[name], (Date, DateTime)): raise TypeError( "'{name}' field must be a '{dt}', not: '{dshape}'".format( name=name, dt=DateTime(), dshape=measure[name], ), )
def _check_datetime_field(name, measure): """Check that a field is a datetime inside some measure. Parameters ---------- name : str The name of the field to check. measure : Record The record to check the field of. Raises ------ TypeError If the field is not a datetime inside ``measure``. """ if not isinstance(measure[name], (Date, DateTime)): raise TypeError( "'{name}' field must be a '{dt}', not: '{dshape}'".format( name=name, dt=DateTime(), dshape=measure[name], ), )
[ "Check", "that", "a", "field", "is", "a", "datetime", "inside", "some", "measure", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L365-L387
[ "def", "_check_datetime_field", "(", "name", ",", "measure", ")", ":", "if", "not", "isinstance", "(", "measure", "[", "name", "]", ",", "(", "Date", ",", "DateTime", ")", ")", ":", "raise", "TypeError", "(", "\"'{name}' field must be a '{dt}', not: '{dshape}'\"...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
_get_metadata
Find the correct metadata expression for the expression. Parameters ---------- field : {'deltas', 'checkpoints'} The kind of metadata expr to lookup. expr : Expr The baseline expression. metadata_expr : Expr, 'auto', or None The metadata argument. If this is 'auto', then the metadata table will be searched for by walking up the expression tree. If this cannot be reflected, then an action will be taken based on the ``no_metadata_rule``. no_metadata_rule : {'warn', 'raise', 'ignore'} How to handle the case where the metadata_expr='auto' but no expr could be found. Returns ------- metadata : Expr or None The deltas or metadata table to use.
zipline/pipeline/loaders/blaze/core.py
def _get_metadata(field, expr, metadata_expr, no_metadata_rule): """Find the correct metadata expression for the expression. Parameters ---------- field : {'deltas', 'checkpoints'} The kind of metadata expr to lookup. expr : Expr The baseline expression. metadata_expr : Expr, 'auto', or None The metadata argument. If this is 'auto', then the metadata table will be searched for by walking up the expression tree. If this cannot be reflected, then an action will be taken based on the ``no_metadata_rule``. no_metadata_rule : {'warn', 'raise', 'ignore'} How to handle the case where the metadata_expr='auto' but no expr could be found. Returns ------- metadata : Expr or None The deltas or metadata table to use. """ if isinstance(metadata_expr, bz.Expr) or metadata_expr is None: return metadata_expr try: return expr._child['_'.join(((expr._name or ''), field))] except (ValueError, AttributeError): if no_metadata_rule == 'raise': raise ValueError( "no %s table could be reflected for %s" % (field, expr) ) elif no_metadata_rule == 'warn': warnings.warn(NoMetaDataWarning(expr, field), stacklevel=4) return None
def _get_metadata(field, expr, metadata_expr, no_metadata_rule): """Find the correct metadata expression for the expression. Parameters ---------- field : {'deltas', 'checkpoints'} The kind of metadata expr to lookup. expr : Expr The baseline expression. metadata_expr : Expr, 'auto', or None The metadata argument. If this is 'auto', then the metadata table will be searched for by walking up the expression tree. If this cannot be reflected, then an action will be taken based on the ``no_metadata_rule``. no_metadata_rule : {'warn', 'raise', 'ignore'} How to handle the case where the metadata_expr='auto' but no expr could be found. Returns ------- metadata : Expr or None The deltas or metadata table to use. """ if isinstance(metadata_expr, bz.Expr) or metadata_expr is None: return metadata_expr try: return expr._child['_'.join(((expr._name or ''), field))] except (ValueError, AttributeError): if no_metadata_rule == 'raise': raise ValueError( "no %s table could be reflected for %s" % (field, expr) ) elif no_metadata_rule == 'warn': warnings.warn(NoMetaDataWarning(expr, field), stacklevel=4) return None
[ "Find", "the", "correct", "metadata", "expression", "for", "the", "expression", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L415-L450
[ "def", "_get_metadata", "(", "field", ",", "expr", ",", "metadata_expr", ",", "no_metadata_rule", ")", ":", "if", "isinstance", "(", "metadata_expr", ",", "bz", ".", "Expr", ")", "or", "metadata_expr", "is", "None", ":", "return", "metadata_expr", "try", ":"...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
_ensure_timestamp_field
Verify that the baseline and deltas expressions have a timestamp field. If there is not a ``TS_FIELD_NAME`` on either of the expressions, it will be copied from the ``AD_FIELD_NAME``. If one is provided, then we will verify that it is the correct dshape. Parameters ---------- dataset_expr : Expr The baseline expression. deltas : Expr or None The deltas expression if any was provided. checkpoints : Expr or None The checkpoints expression if any was provided. Returns ------- dataset_expr, deltas : Expr The new baseline and deltas expressions to use.
zipline/pipeline/loaders/blaze/core.py
def _ensure_timestamp_field(dataset_expr, deltas, checkpoints): """Verify that the baseline and deltas expressions have a timestamp field. If there is not a ``TS_FIELD_NAME`` on either of the expressions, it will be copied from the ``AD_FIELD_NAME``. If one is provided, then we will verify that it is the correct dshape. Parameters ---------- dataset_expr : Expr The baseline expression. deltas : Expr or None The deltas expression if any was provided. checkpoints : Expr or None The checkpoints expression if any was provided. Returns ------- dataset_expr, deltas : Expr The new baseline and deltas expressions to use. """ measure = dataset_expr.dshape.measure if TS_FIELD_NAME not in measure.names: dataset_expr = bz.transform( dataset_expr, **{TS_FIELD_NAME: dataset_expr[AD_FIELD_NAME]} ) deltas = _ad_as_ts(deltas) checkpoints = _ad_as_ts(checkpoints) else: _check_datetime_field(TS_FIELD_NAME, measure) return dataset_expr, deltas, checkpoints
def _ensure_timestamp_field(dataset_expr, deltas, checkpoints): """Verify that the baseline and deltas expressions have a timestamp field. If there is not a ``TS_FIELD_NAME`` on either of the expressions, it will be copied from the ``AD_FIELD_NAME``. If one is provided, then we will verify that it is the correct dshape. Parameters ---------- dataset_expr : Expr The baseline expression. deltas : Expr or None The deltas expression if any was provided. checkpoints : Expr or None The checkpoints expression if any was provided. Returns ------- dataset_expr, deltas : Expr The new baseline and deltas expressions to use. """ measure = dataset_expr.dshape.measure if TS_FIELD_NAME not in measure.names: dataset_expr = bz.transform( dataset_expr, **{TS_FIELD_NAME: dataset_expr[AD_FIELD_NAME]} ) deltas = _ad_as_ts(deltas) checkpoints = _ad_as_ts(checkpoints) else: _check_datetime_field(TS_FIELD_NAME, measure) return dataset_expr, deltas, checkpoints
[ "Verify", "that", "the", "baseline", "and", "deltas", "expressions", "have", "a", "timestamp", "field", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L473-L505
[ "def", "_ensure_timestamp_field", "(", "dataset_expr", ",", "deltas", ",", "checkpoints", ")", ":", "measure", "=", "dataset_expr", ".", "dshape", ".", "measure", "if", "TS_FIELD_NAME", "not", "in", "measure", ".", "names", ":", "dataset_expr", "=", "bz", ".",...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
from_blaze
Create a Pipeline API object from a blaze expression. Parameters ---------- expr : Expr The blaze expression to use. deltas : Expr, 'auto' or None, optional The expression to use for the point in time adjustments. If the string 'auto' is passed, a deltas expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_deltas'. If None is passed, no deltas will be used. checkpoints : Expr, 'auto' or None, optional The expression to use for the forward fill checkpoints. If the string 'auto' is passed, a checkpoints expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_checkpoints'. If None is passed, no checkpoints will be used. loader : BlazeLoader, optional The blaze loader to attach this pipeline dataset to. If None is passed, the global blaze loader is used. resources : dict or any, optional The data to execute the blaze expressions against. This is used as the scope for ``bz.compute``. odo_kwargs : dict, optional The keyword arguments to pass to odo when evaluating the expressions. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. missing_values : dict[str -> any], optional A dict mapping column names to missing values for those columns. Missing values are required for integral columns. no_deltas_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``deltas='auto'`` but no deltas can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. no_checkpoints_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``checkpoints='auto'`` but no checkpoints can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. Returns ------- pipeline_api_obj : DataSet or BoundColumn Either a new dataset or bound column based on the shape of the expr passed in. If a table shaped expression is passed, this will return a ``DataSet`` that represents the whole table. If an array-like shape is passed, a ``BoundColumn`` on the dataset that would be constructed from passing the parent is returned.
zipline/pipeline/loaders/blaze/core.py
def from_blaze(expr, deltas='auto', checkpoints='auto', loader=None, resources=None, odo_kwargs=None, missing_values=None, domain=GENERIC, no_deltas_rule='warn', no_checkpoints_rule='warn'): """Create a Pipeline API object from a blaze expression. Parameters ---------- expr : Expr The blaze expression to use. deltas : Expr, 'auto' or None, optional The expression to use for the point in time adjustments. If the string 'auto' is passed, a deltas expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_deltas'. If None is passed, no deltas will be used. checkpoints : Expr, 'auto' or None, optional The expression to use for the forward fill checkpoints. If the string 'auto' is passed, a checkpoints expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_checkpoints'. If None is passed, no checkpoints will be used. loader : BlazeLoader, optional The blaze loader to attach this pipeline dataset to. If None is passed, the global blaze loader is used. resources : dict or any, optional The data to execute the blaze expressions against. This is used as the scope for ``bz.compute``. odo_kwargs : dict, optional The keyword arguments to pass to odo when evaluating the expressions. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. missing_values : dict[str -> any], optional A dict mapping column names to missing values for those columns. Missing values are required for integral columns. no_deltas_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``deltas='auto'`` but no deltas can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. no_checkpoints_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``checkpoints='auto'`` but no checkpoints can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. Returns ------- pipeline_api_obj : DataSet or BoundColumn Either a new dataset or bound column based on the shape of the expr passed in. If a table shaped expression is passed, this will return a ``DataSet`` that represents the whole table. If an array-like shape is passed, a ``BoundColumn`` on the dataset that would be constructed from passing the parent is returned. """ if 'auto' in {deltas, checkpoints}: invalid_nodes = tuple(filter(is_invalid_deltas_node, expr._subterms())) if invalid_nodes: raise TypeError( 'expression with auto %s may only contain (%s) nodes,' " found: %s" % ( ' or '.join( ['deltas'] if deltas is not None else [] + ['checkpoints'] if checkpoints is not None else [], ), ', '.join(map(get__name__, valid_deltas_node_types)), ', '.join( set(map(compose(get__name__, type), invalid_nodes)), ), ), ) deltas = _get_metadata( 'deltas', expr, deltas, no_deltas_rule, ) checkpoints = _get_metadata( 'checkpoints', expr, checkpoints, no_checkpoints_rule, ) # Check if this is a single column out of a dataset. if bz.ndim(expr) != 1: raise TypeError( 'expression was not tabular or array-like,' ' %s dimensions: %d' % ( 'too many' if bz.ndim(expr) > 1 else 'not enough', bz.ndim(expr), ), ) single_column = None if isscalar(expr.dshape.measure): # This is a single column. Record which column we are to return # but create the entire dataset. single_column = rename = expr._name field_hit = False if not isinstance(expr, traversable_nodes): raise TypeError( "expression '%s' was array-like but not a simple field of" " some larger table" % str(expr), ) while isinstance(expr, traversable_nodes): if isinstance(expr, bz.expr.Field): if not field_hit: field_hit = True else: break rename = expr._name expr = expr._child dataset_expr = expr.relabel({rename: single_column}) else: dataset_expr = expr measure = dataset_expr.dshape.measure if not isrecord(measure) or AD_FIELD_NAME not in measure.names: raise TypeError( "The dataset must be a collection of records with at least an" " '{ad}' field. Fields provided: '{fields}'\nhint: maybe you need" " to use `relabel` to change your field names".format( ad=AD_FIELD_NAME, fields=measure, ), ) _check_datetime_field(AD_FIELD_NAME, measure) dataset_expr, deltas, checkpoints = _ensure_timestamp_field( dataset_expr, deltas, checkpoints, ) if deltas is not None and (sorted(deltas.dshape.measure.fields) != sorted(measure.fields)): raise TypeError( 'baseline measure != deltas measure:\n%s != %s' % ( measure, deltas.dshape.measure, ), ) if (checkpoints is not None and (sorted(checkpoints.dshape.measure.fields) != sorted(measure.fields))): raise TypeError( 'baseline measure != checkpoints measure:\n%s != %s' % ( measure, checkpoints.dshape.measure, ), ) # Ensure that we have a data resource to execute the query against. _check_resources('expr', dataset_expr, resources) _check_resources('deltas', deltas, resources) _check_resources('checkpoints', checkpoints, resources) # Create or retrieve the Pipeline API dataset. if missing_values is None: missing_values = {} ds = new_dataset(dataset_expr, frozenset(missing_values.items()), domain) # Register our new dataset with the loader. (loader if loader is not None else global_loader).register_dataset( ds, bind_expression_to_resources(dataset_expr, resources), bind_expression_to_resources(deltas, resources) if deltas is not None else None, bind_expression_to_resources(checkpoints, resources) if checkpoints is not None else None, odo_kwargs=odo_kwargs, ) if single_column is not None: # We were passed a single column, extract and return it. return getattr(ds, single_column) return ds
def from_blaze(expr, deltas='auto', checkpoints='auto', loader=None, resources=None, odo_kwargs=None, missing_values=None, domain=GENERIC, no_deltas_rule='warn', no_checkpoints_rule='warn'): """Create a Pipeline API object from a blaze expression. Parameters ---------- expr : Expr The blaze expression to use. deltas : Expr, 'auto' or None, optional The expression to use for the point in time adjustments. If the string 'auto' is passed, a deltas expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_deltas'. If None is passed, no deltas will be used. checkpoints : Expr, 'auto' or None, optional The expression to use for the forward fill checkpoints. If the string 'auto' is passed, a checkpoints expr will be looked up by stepping up the expression tree and looking for another field with the name of ``expr._name`` + '_checkpoints'. If None is passed, no checkpoints will be used. loader : BlazeLoader, optional The blaze loader to attach this pipeline dataset to. If None is passed, the global blaze loader is used. resources : dict or any, optional The data to execute the blaze expressions against. This is used as the scope for ``bz.compute``. odo_kwargs : dict, optional The keyword arguments to pass to odo when evaluating the expressions. domain : zipline.pipeline.domain.Domain Domain of the dataset to be created. missing_values : dict[str -> any], optional A dict mapping column names to missing values for those columns. Missing values are required for integral columns. no_deltas_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``deltas='auto'`` but no deltas can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. no_checkpoints_rule : {'warn', 'raise', 'ignore'}, optional What should happen if ``checkpoints='auto'`` but no checkpoints can be found. 'warn' says to raise a warning but continue. 'raise' says to raise an exception if no deltas can be found. 'ignore' says take no action and proceed with no deltas. Returns ------- pipeline_api_obj : DataSet or BoundColumn Either a new dataset or bound column based on the shape of the expr passed in. If a table shaped expression is passed, this will return a ``DataSet`` that represents the whole table. If an array-like shape is passed, a ``BoundColumn`` on the dataset that would be constructed from passing the parent is returned. """ if 'auto' in {deltas, checkpoints}: invalid_nodes = tuple(filter(is_invalid_deltas_node, expr._subterms())) if invalid_nodes: raise TypeError( 'expression with auto %s may only contain (%s) nodes,' " found: %s" % ( ' or '.join( ['deltas'] if deltas is not None else [] + ['checkpoints'] if checkpoints is not None else [], ), ', '.join(map(get__name__, valid_deltas_node_types)), ', '.join( set(map(compose(get__name__, type), invalid_nodes)), ), ), ) deltas = _get_metadata( 'deltas', expr, deltas, no_deltas_rule, ) checkpoints = _get_metadata( 'checkpoints', expr, checkpoints, no_checkpoints_rule, ) # Check if this is a single column out of a dataset. if bz.ndim(expr) != 1: raise TypeError( 'expression was not tabular or array-like,' ' %s dimensions: %d' % ( 'too many' if bz.ndim(expr) > 1 else 'not enough', bz.ndim(expr), ), ) single_column = None if isscalar(expr.dshape.measure): # This is a single column. Record which column we are to return # but create the entire dataset. single_column = rename = expr._name field_hit = False if not isinstance(expr, traversable_nodes): raise TypeError( "expression '%s' was array-like but not a simple field of" " some larger table" % str(expr), ) while isinstance(expr, traversable_nodes): if isinstance(expr, bz.expr.Field): if not field_hit: field_hit = True else: break rename = expr._name expr = expr._child dataset_expr = expr.relabel({rename: single_column}) else: dataset_expr = expr measure = dataset_expr.dshape.measure if not isrecord(measure) or AD_FIELD_NAME not in measure.names: raise TypeError( "The dataset must be a collection of records with at least an" " '{ad}' field. Fields provided: '{fields}'\nhint: maybe you need" " to use `relabel` to change your field names".format( ad=AD_FIELD_NAME, fields=measure, ), ) _check_datetime_field(AD_FIELD_NAME, measure) dataset_expr, deltas, checkpoints = _ensure_timestamp_field( dataset_expr, deltas, checkpoints, ) if deltas is not None and (sorted(deltas.dshape.measure.fields) != sorted(measure.fields)): raise TypeError( 'baseline measure != deltas measure:\n%s != %s' % ( measure, deltas.dshape.measure, ), ) if (checkpoints is not None and (sorted(checkpoints.dshape.measure.fields) != sorted(measure.fields))): raise TypeError( 'baseline measure != checkpoints measure:\n%s != %s' % ( measure, checkpoints.dshape.measure, ), ) # Ensure that we have a data resource to execute the query against. _check_resources('expr', dataset_expr, resources) _check_resources('deltas', deltas, resources) _check_resources('checkpoints', checkpoints, resources) # Create or retrieve the Pipeline API dataset. if missing_values is None: missing_values = {} ds = new_dataset(dataset_expr, frozenset(missing_values.items()), domain) # Register our new dataset with the loader. (loader if loader is not None else global_loader).register_dataset( ds, bind_expression_to_resources(dataset_expr, resources), bind_expression_to_resources(deltas, resources) if deltas is not None else None, bind_expression_to_resources(checkpoints, resources) if checkpoints is not None else None, odo_kwargs=odo_kwargs, ) if single_column is not None: # We were passed a single column, extract and return it. return getattr(ds, single_column) return ds
[ "Create", "a", "Pipeline", "API", "object", "from", "a", "blaze", "expression", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L512-L695
[ "def", "from_blaze", "(", "expr", ",", "deltas", "=", "'auto'", ",", "checkpoints", "=", "'auto'", ",", "loader", "=", "None", ",", "resources", "=", "None", ",", "odo_kwargs", "=", "None", ",", "missing_values", "=", "None", ",", "domain", "=", "GENERIC...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
bind_expression_to_resources
Bind a Blaze expression to resources. Parameters ---------- expr : bz.Expr The expression to which we want to bind resources. resources : dict[bz.Symbol -> any] Mapping from the loadable terms of ``expr`` to actual data resources. Returns ------- bound_expr : bz.Expr ``expr`` with bound resources.
zipline/pipeline/loaders/blaze/core.py
def bind_expression_to_resources(expr, resources): """ Bind a Blaze expression to resources. Parameters ---------- expr : bz.Expr The expression to which we want to bind resources. resources : dict[bz.Symbol -> any] Mapping from the loadable terms of ``expr`` to actual data resources. Returns ------- bound_expr : bz.Expr ``expr`` with bound resources. """ # bind the resources into the expression if resources is None: resources = {} # _subs stands for substitute. It's not actually private, blaze just # prefixes symbol-manipulation methods with underscores to prevent # collisions with data column names. return expr._subs({ k: bz.data(v, dshape=k.dshape) for k, v in iteritems(resources) })
def bind_expression_to_resources(expr, resources): """ Bind a Blaze expression to resources. Parameters ---------- expr : bz.Expr The expression to which we want to bind resources. resources : dict[bz.Symbol -> any] Mapping from the loadable terms of ``expr`` to actual data resources. Returns ------- bound_expr : bz.Expr ``expr`` with bound resources. """ # bind the resources into the expression if resources is None: resources = {} # _subs stands for substitute. It's not actually private, blaze just # prefixes symbol-manipulation methods with underscores to prevent # collisions with data column names. return expr._subs({ k: bz.data(v, dshape=k.dshape) for k, v in iteritems(resources) })
[ "Bind", "a", "Blaze", "expression", "to", "resources", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L1038-L1063
[ "def", "bind_expression_to_resources", "(", "expr", ",", "resources", ")", ":", "# bind the resources into the expression", "if", "resources", "is", "None", ":", "resources", "=", "{", "}", "# _subs stands for substitute. It's not actually private, blaze just", "# prefixes sym...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
get_materialized_checkpoints
Computes a lower bound and a DataFrame checkpoints. Parameters ---------- checkpoints : Expr Bound blaze expression for a checkpoints table from which to get a computed lower bound. colnames : iterable of str The names of the columns for which checkpoints should be computed. lower_dt : pd.Timestamp The lower date being queried for that serves as an upper bound for checkpoints. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``.
zipline/pipeline/loaders/blaze/core.py
def get_materialized_checkpoints(checkpoints, colnames, lower_dt, odo_kwargs): """ Computes a lower bound and a DataFrame checkpoints. Parameters ---------- checkpoints : Expr Bound blaze expression for a checkpoints table from which to get a computed lower bound. colnames : iterable of str The names of the columns for which checkpoints should be computed. lower_dt : pd.Timestamp The lower date being queried for that serves as an upper bound for checkpoints. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``. """ if checkpoints is not None: ts = checkpoints[TS_FIELD_NAME] checkpoints_ts = odo( ts[ts < lower_dt].max(), pd.Timestamp, **odo_kwargs ) if pd.isnull(checkpoints_ts): # We don't have a checkpoint for before our start date so just # don't constrain the lower date. materialized_checkpoints = pd.DataFrame(columns=colnames) lower = None else: materialized_checkpoints = odo( checkpoints[ts == checkpoints_ts][colnames], pd.DataFrame, **odo_kwargs ) lower = checkpoints_ts else: materialized_checkpoints = pd.DataFrame(columns=colnames) lower = None # we don't have a good lower date constraint return lower, materialized_checkpoints
def get_materialized_checkpoints(checkpoints, colnames, lower_dt, odo_kwargs): """ Computes a lower bound and a DataFrame checkpoints. Parameters ---------- checkpoints : Expr Bound blaze expression for a checkpoints table from which to get a computed lower bound. colnames : iterable of str The names of the columns for which checkpoints should be computed. lower_dt : pd.Timestamp The lower date being queried for that serves as an upper bound for checkpoints. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``. """ if checkpoints is not None: ts = checkpoints[TS_FIELD_NAME] checkpoints_ts = odo( ts[ts < lower_dt].max(), pd.Timestamp, **odo_kwargs ) if pd.isnull(checkpoints_ts): # We don't have a checkpoint for before our start date so just # don't constrain the lower date. materialized_checkpoints = pd.DataFrame(columns=colnames) lower = None else: materialized_checkpoints = odo( checkpoints[ts == checkpoints_ts][colnames], pd.DataFrame, **odo_kwargs ) lower = checkpoints_ts else: materialized_checkpoints = pd.DataFrame(columns=colnames) lower = None # we don't have a good lower date constraint return lower, materialized_checkpoints
[ "Computes", "a", "lower", "bound", "and", "a", "DataFrame", "checkpoints", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L1066-L1105
[ "def", "get_materialized_checkpoints", "(", "checkpoints", ",", "colnames", ",", "lower_dt", ",", "odo_kwargs", ")", ":", "if", "checkpoints", "is", "not", "None", ":", "ts", "=", "checkpoints", "[", "TS_FIELD_NAME", "]", "checkpoints_ts", "=", "odo", "(", "ts...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
ffill_query_in_range
Query a blaze expression in a given time range properly forward filling from values that fall before the lower date. Parameters ---------- expr : Expr Bound blaze expression. lower : datetime The lower date to query for. upper : datetime The upper date to query for. checkpoints : Expr, optional Bound blaze expression for a checkpoints table from which to get a computed lower bound. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``. ts_field : str, optional The name of the timestamp field in the given blaze expression. Returns ------- raw : pd.DataFrame A strict dataframe for the data in the given date range. This may start before the requested start date if a value is needed to ffill.
zipline/pipeline/loaders/blaze/core.py
def ffill_query_in_range(expr, lower, upper, checkpoints=None, odo_kwargs=None, ts_field=TS_FIELD_NAME): """Query a blaze expression in a given time range properly forward filling from values that fall before the lower date. Parameters ---------- expr : Expr Bound blaze expression. lower : datetime The lower date to query for. upper : datetime The upper date to query for. checkpoints : Expr, optional Bound blaze expression for a checkpoints table from which to get a computed lower bound. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``. ts_field : str, optional The name of the timestamp field in the given blaze expression. Returns ------- raw : pd.DataFrame A strict dataframe for the data in the given date range. This may start before the requested start date if a value is needed to ffill. """ odo_kwargs = odo_kwargs or {} computed_lower, materialized_checkpoints = get_materialized_checkpoints( checkpoints, expr.fields, lower, odo_kwargs, ) pred = expr[ts_field] <= upper if computed_lower is not None: # only constrain the lower date if we computed a new lower date pred &= expr[ts_field] >= computed_lower raw = pd.concat( ( materialized_checkpoints, odo( expr[pred], pd.DataFrame, **odo_kwargs ), ), ignore_index=True, ) raw.loc[:, ts_field] = raw.loc[:, ts_field].astype('datetime64[ns]') return raw
def ffill_query_in_range(expr, lower, upper, checkpoints=None, odo_kwargs=None, ts_field=TS_FIELD_NAME): """Query a blaze expression in a given time range properly forward filling from values that fall before the lower date. Parameters ---------- expr : Expr Bound blaze expression. lower : datetime The lower date to query for. upper : datetime The upper date to query for. checkpoints : Expr, optional Bound blaze expression for a checkpoints table from which to get a computed lower bound. odo_kwargs : dict, optional The extra keyword arguments to pass to ``odo``. ts_field : str, optional The name of the timestamp field in the given blaze expression. Returns ------- raw : pd.DataFrame A strict dataframe for the data in the given date range. This may start before the requested start date if a value is needed to ffill. """ odo_kwargs = odo_kwargs or {} computed_lower, materialized_checkpoints = get_materialized_checkpoints( checkpoints, expr.fields, lower, odo_kwargs, ) pred = expr[ts_field] <= upper if computed_lower is not None: # only constrain the lower date if we computed a new lower date pred &= expr[ts_field] >= computed_lower raw = pd.concat( ( materialized_checkpoints, odo( expr[pred], pd.DataFrame, **odo_kwargs ), ), ignore_index=True, ) raw.loc[:, ts_field] = raw.loc[:, ts_field].astype('datetime64[ns]') return raw
[ "Query", "a", "blaze", "expression", "in", "a", "given", "time", "range", "properly", "forward", "filling", "from", "values", "that", "fall", "before", "the", "lower", "date", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L1108-L1165
[ "def", "ffill_query_in_range", "(", "expr", ",", "lower", ",", "upper", ",", "checkpoints", "=", "None", ",", "odo_kwargs", "=", "None", ",", "ts_field", "=", "TS_FIELD_NAME", ")", ":", "odo_kwargs", "=", "odo_kwargs", "or", "{", "}", "computed_lower", ",", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
BlazeLoader.register_dataset
Explicitly map a datset to a collection of blaze expressions. Parameters ---------- dataset : DataSet The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze`
zipline/pipeline/loaders/blaze/core.py
def register_dataset(self, dataset, expr, deltas=None, checkpoints=None, odo_kwargs=None): """Explicitly map a datset to a collection of blaze expressions. Parameters ---------- dataset : DataSet The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze` """ expr_data = ExprData( expr, deltas, checkpoints, odo_kwargs, ) for column in dataset.columns: self._table_expressions[column] = expr_data
def register_dataset(self, dataset, expr, deltas=None, checkpoints=None, odo_kwargs=None): """Explicitly map a datset to a collection of blaze expressions. Parameters ---------- dataset : DataSet The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze` """ expr_data = ExprData( expr, deltas, checkpoints, odo_kwargs, ) for column in dataset.columns: self._table_expressions[column] = expr_data
[ "Explicitly", "map", "a", "datset", "to", "a", "collection", "of", "blaze", "expressions", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L847-L879
[ "def", "register_dataset", "(", "self", ",", "dataset", ",", "expr", ",", "deltas", "=", "None", ",", "checkpoints", "=", "None", ",", "odo_kwargs", "=", "None", ")", ":", "expr_data", "=", "ExprData", "(", "expr", ",", "deltas", ",", "checkpoints", ",",...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
BlazeLoader.register_column
Explicitly map a single bound column to a collection of blaze expressions. The expressions need to have ``timestamp`` and ``as_of`` columns. Parameters ---------- column : BoundColumn The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze`
zipline/pipeline/loaders/blaze/core.py
def register_column(self, column, expr, deltas=None, checkpoints=None, odo_kwargs=None): """Explicitly map a single bound column to a collection of blaze expressions. The expressions need to have ``timestamp`` and ``as_of`` columns. Parameters ---------- column : BoundColumn The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze` """ self._table_expressions[column] = ExprData( expr, deltas, checkpoints, odo_kwargs, )
def register_column(self, column, expr, deltas=None, checkpoints=None, odo_kwargs=None): """Explicitly map a single bound column to a collection of blaze expressions. The expressions need to have ``timestamp`` and ``as_of`` columns. Parameters ---------- column : BoundColumn The pipeline dataset to map to the given expressions. expr : Expr The baseline values. deltas : Expr, optional The deltas for the data. checkpoints : Expr, optional The forward fill checkpoints for the data. odo_kwargs : dict, optional The keyword arguments to forward to the odo calls internally. See Also -------- :func:`zipline.pipeline.loaders.blaze.from_blaze` """ self._table_expressions[column] = ExprData( expr, deltas, checkpoints, odo_kwargs, )
[ "Explicitly", "map", "a", "single", "bound", "column", "to", "a", "collection", "of", "blaze", "expressions", ".", "The", "expressions", "need", "to", "have", "timestamp", "and", "as_of", "columns", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/pipeline/loaders/blaze/core.py#L881-L913
[ "def", "register_column", "(", "self", ",", "column", ",", "expr", ",", "deltas", "=", "None", ",", "checkpoints", "=", "None", ",", "odo_kwargs", "=", "None", ")", ":", "self", ".", "_table_expressions", "[", "column", "]", "=", "ExprData", "(", "expr",...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
merge_ownership_periods
Given a dict of mappings where the values are lists of OwnershipPeriod objects, returns a dict with the same structure with new OwnershipPeriod objects adjusted so that the periods have no gaps. Orders the periods chronologically, and pushes forward the end date of each period to match the start date of the following period. The end date of the last period pushed forward to the max Timestamp.
zipline/assets/assets.py
def merge_ownership_periods(mappings): """ Given a dict of mappings where the values are lists of OwnershipPeriod objects, returns a dict with the same structure with new OwnershipPeriod objects adjusted so that the periods have no gaps. Orders the periods chronologically, and pushes forward the end date of each period to match the start date of the following period. The end date of the last period pushed forward to the max Timestamp. """ return valmap( lambda v: tuple( OwnershipPeriod( a.start, b.start, a.sid, a.value, ) for a, b in sliding_window( 2, concatv( sorted(v), # concat with a fake ownership object to make the last # end date be max timestamp [OwnershipPeriod( pd.Timestamp.max.tz_localize('utc'), None, None, None, )], ), ) ), mappings, )
def merge_ownership_periods(mappings): """ Given a dict of mappings where the values are lists of OwnershipPeriod objects, returns a dict with the same structure with new OwnershipPeriod objects adjusted so that the periods have no gaps. Orders the periods chronologically, and pushes forward the end date of each period to match the start date of the following period. The end date of the last period pushed forward to the max Timestamp. """ return valmap( lambda v: tuple( OwnershipPeriod( a.start, b.start, a.sid, a.value, ) for a, b in sliding_window( 2, concatv( sorted(v), # concat with a fake ownership object to make the last # end date be max timestamp [OwnershipPeriod( pd.Timestamp.max.tz_localize('utc'), None, None, None, )], ), ) ), mappings, )
[ "Given", "a", "dict", "of", "mappings", "where", "the", "values", "are", "lists", "of", "OwnershipPeriod", "objects", "returns", "a", "dict", "with", "the", "same", "structure", "with", "new", "OwnershipPeriod", "objects", "adjusted", "so", "that", "the", "per...
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L104-L138
[ "def", "merge_ownership_periods", "(", "mappings", ")", ":", "return", "valmap", "(", "lambda", "v", ":", "tuple", "(", "OwnershipPeriod", "(", "a", ".", "start", ",", "b", ".", "start", ",", "a", ".", "sid", ",", "a", ".", "value", ",", ")", "for", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
build_ownership_map
Builds a dict mapping to lists of OwnershipPeriods, from a db table.
zipline/assets/assets.py
def build_ownership_map(table, key_from_row, value_from_row): """ Builds a dict mapping to lists of OwnershipPeriods, from a db table. """ return _build_ownership_map_from_rows( sa.select(table.c).execute().fetchall(), key_from_row, value_from_row, )
def build_ownership_map(table, key_from_row, value_from_row): """ Builds a dict mapping to lists of OwnershipPeriods, from a db table. """ return _build_ownership_map_from_rows( sa.select(table.c).execute().fetchall(), key_from_row, value_from_row, )
[ "Builds", "a", "dict", "mapping", "to", "lists", "of", "OwnershipPeriods", "from", "a", "db", "table", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L159-L167
[ "def", "build_ownership_map", "(", "table", ",", "key_from_row", ",", "value_from_row", ")", ":", "return", "_build_ownership_map_from_rows", "(", "sa", ".", "select", "(", "table", ".", "c", ")", ".", "execute", "(", ")", ".", "fetchall", "(", ")", ",", "...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
build_grouped_ownership_map
Builds a dict mapping group keys to maps of keys to to lists of OwnershipPeriods, from a db table.
zipline/assets/assets.py
def build_grouped_ownership_map(table, key_from_row, value_from_row, group_key): """ Builds a dict mapping group keys to maps of keys to to lists of OwnershipPeriods, from a db table. """ grouped_rows = groupby( group_key, sa.select(table.c).execute().fetchall(), ) return { key: _build_ownership_map_from_rows( rows, key_from_row, value_from_row, ) for key, rows in grouped_rows.items() }
def build_grouped_ownership_map(table, key_from_row, value_from_row, group_key): """ Builds a dict mapping group keys to maps of keys to to lists of OwnershipPeriods, from a db table. """ grouped_rows = groupby( group_key, sa.select(table.c).execute().fetchall(), ) return { key: _build_ownership_map_from_rows( rows, key_from_row, value_from_row, ) for key, rows in grouped_rows.items() }
[ "Builds", "a", "dict", "mapping", "group", "keys", "to", "maps", "of", "keys", "to", "to", "lists", "of", "OwnershipPeriods", "from", "a", "db", "table", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L170-L189
[ "def", "build_grouped_ownership_map", "(", "table", ",", "key_from_row", ",", "value_from_row", ",", "group_key", ")", ":", "grouped_rows", "=", "groupby", "(", "group_key", ",", "sa", ".", "select", "(", "table", ".", "c", ")", ".", "execute", "(", ")", "...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
_filter_kwargs
Filter out kwargs from a dictionary. Parameters ---------- names : set[str] The names to select from ``dict_``. dict_ : dict[str, any] The dictionary to select from. Returns ------- kwargs : dict[str, any] ``dict_`` where the keys intersect with ``names`` and the values are not None.
zipline/assets/assets.py
def _filter_kwargs(names, dict_): """Filter out kwargs from a dictionary. Parameters ---------- names : set[str] The names to select from ``dict_``. dict_ : dict[str, any] The dictionary to select from. Returns ------- kwargs : dict[str, any] ``dict_`` where the keys intersect with ``names`` and the values are not None. """ return {k: v for k, v in dict_.items() if k in names and v is not None}
def _filter_kwargs(names, dict_): """Filter out kwargs from a dictionary. Parameters ---------- names : set[str] The names to select from ``dict_``. dict_ : dict[str, any] The dictionary to select from. Returns ------- kwargs : dict[str, any] ``dict_`` where the keys intersect with ``names`` and the values are not None. """ return {k: v for k, v in dict_.items() if k in names and v is not None}
[ "Filter", "out", "kwargs", "from", "a", "dictionary", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L193-L209
[ "def", "_filter_kwargs", "(", "names", ",", "dict_", ")", ":", "return", "{", "k", ":", "v", "for", "k", ",", "v", "in", "dict_", ".", "items", "(", ")", "if", "k", "in", "names", "and", "v", "is", "not", "None", "}" ]
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
_convert_asset_timestamp_fields
Takes in a dict of Asset init args and converts dates to pd.Timestamps
zipline/assets/assets.py
def _convert_asset_timestamp_fields(dict_): """ Takes in a dict of Asset init args and converts dates to pd.Timestamps """ for key in _asset_timestamp_fields & viewkeys(dict_): value = pd.Timestamp(dict_[key], tz='UTC') dict_[key] = None if isnull(value) else value return dict_
def _convert_asset_timestamp_fields(dict_): """ Takes in a dict of Asset init args and converts dates to pd.Timestamps """ for key in _asset_timestamp_fields & viewkeys(dict_): value = pd.Timestamp(dict_[key], tz='UTC') dict_[key] = None if isnull(value) else value return dict_
[ "Takes", "in", "a", "dict", "of", "Asset", "init", "args", "and", "converts", "dates", "to", "pd", ".", "Timestamps" ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L216-L223
[ "def", "_convert_asset_timestamp_fields", "(", "dict_", ")", ":", "for", "key", "in", "_asset_timestamp_fields", "&", "viewkeys", "(", "dict_", ")", ":", "value", "=", "pd", ".", "Timestamp", "(", "dict_", "[", "key", "]", ",", "tz", "=", "'UTC'", ")", "...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
was_active
Whether or not `asset` was active at the time corresponding to `reference_date_value`. Parameters ---------- reference_date_value : int Date, represented as nanoseconds since EPOCH, for which we want to know if `asset` was alive. This is generally the result of accessing the `value` attribute of a pandas Timestamp. asset : Asset The asset object to check. Returns ------- was_active : bool Whether or not the `asset` existed at the specified time.
zipline/assets/assets.py
def was_active(reference_date_value, asset): """ Whether or not `asset` was active at the time corresponding to `reference_date_value`. Parameters ---------- reference_date_value : int Date, represented as nanoseconds since EPOCH, for which we want to know if `asset` was alive. This is generally the result of accessing the `value` attribute of a pandas Timestamp. asset : Asset The asset object to check. Returns ------- was_active : bool Whether or not the `asset` existed at the specified time. """ return ( asset.start_date.value <= reference_date_value <= asset.end_date.value )
def was_active(reference_date_value, asset): """ Whether or not `asset` was active at the time corresponding to `reference_date_value`. Parameters ---------- reference_date_value : int Date, represented as nanoseconds since EPOCH, for which we want to know if `asset` was alive. This is generally the result of accessing the `value` attribute of a pandas Timestamp. asset : Asset The asset object to check. Returns ------- was_active : bool Whether or not the `asset` existed at the specified time. """ return ( asset.start_date.value <= reference_date_value <= asset.end_date.value )
[ "Whether", "or", "not", "asset", "was", "active", "at", "the", "time", "corresponding", "to", "reference_date_value", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L1568-L1591
[ "def", "was_active", "(", "reference_date_value", ",", "asset", ")", ":", "return", "(", "asset", ".", "start_date", ".", "value", "<=", "reference_date_value", "<=", "asset", ".", "end_date", ".", "value", ")" ]
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder.lookup_asset_types
Retrieve asset types for a list of sids. Parameters ---------- sids : list[int] Returns ------- types : dict[sid -> str or None] Asset types for the provided sids.
zipline/assets/assets.py
def lookup_asset_types(self, sids): """ Retrieve asset types for a list of sids. Parameters ---------- sids : list[int] Returns ------- types : dict[sid -> str or None] Asset types for the provided sids. """ found = {} missing = set() for sid in sids: try: found[sid] = self._asset_type_cache[sid] except KeyError: missing.add(sid) if not missing: return found router_cols = self.asset_router.c for assets in group_into_chunks(missing): query = sa.select((router_cols.sid, router_cols.asset_type)).where( self.asset_router.c.sid.in_(map(int, assets)) ) for sid, type_ in query.execute().fetchall(): missing.remove(sid) found[sid] = self._asset_type_cache[sid] = type_ for sid in missing: found[sid] = self._asset_type_cache[sid] = None return found
def lookup_asset_types(self, sids): """ Retrieve asset types for a list of sids. Parameters ---------- sids : list[int] Returns ------- types : dict[sid -> str or None] Asset types for the provided sids. """ found = {} missing = set() for sid in sids: try: found[sid] = self._asset_type_cache[sid] except KeyError: missing.add(sid) if not missing: return found router_cols = self.asset_router.c for assets in group_into_chunks(missing): query = sa.select((router_cols.sid, router_cols.asset_type)).where( self.asset_router.c.sid.in_(map(int, assets)) ) for sid, type_ in query.execute().fetchall(): missing.remove(sid) found[sid] = self._asset_type_cache[sid] = type_ for sid in missing: found[sid] = self._asset_type_cache[sid] = None return found
[ "Retrieve", "asset", "types", "for", "a", "list", "of", "sids", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L405-L443
[ "def", "lookup_asset_types", "(", "self", ",", "sids", ")", ":", "found", "=", "{", "}", "missing", "=", "set", "(", ")", "for", "sid", "in", "sids", ":", "try", ":", "found", "[", "sid", "]", "=", "self", ".", "_asset_type_cache", "[", "sid", "]",...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder.retrieve_all
Retrieve all assets in `sids`. Parameters ---------- sids : iterable of int Assets to retrieve. default_none : bool If True, return None for failed lookups. If False, raise `SidsNotFound`. Returns ------- assets : list[Asset or None] A list of the same length as `sids` containing Assets (or Nones) corresponding to the requested sids. Raises ------ SidsNotFound When a requested sid is not found and default_none=False.
zipline/assets/assets.py
def retrieve_all(self, sids, default_none=False): """ Retrieve all assets in `sids`. Parameters ---------- sids : iterable of int Assets to retrieve. default_none : bool If True, return None for failed lookups. If False, raise `SidsNotFound`. Returns ------- assets : list[Asset or None] A list of the same length as `sids` containing Assets (or Nones) corresponding to the requested sids. Raises ------ SidsNotFound When a requested sid is not found and default_none=False. """ sids = list(sids) hits, missing, failures = {}, set(), [] for sid in sids: try: asset = self._asset_cache[sid] if not default_none and asset is None: # Bail early if we've already cached that we don't know # about an asset. raise SidsNotFound(sids=[sid]) hits[sid] = asset except KeyError: missing.add(sid) # All requests were cache hits. Return requested sids in order. if not missing: return [hits[sid] for sid in sids] update_hits = hits.update # Look up cache misses by type. type_to_assets = self.group_by_type(missing) # Handle failures failures = {failure: None for failure in type_to_assets.pop(None, ())} update_hits(failures) self._asset_cache.update(failures) if failures and not default_none: raise SidsNotFound(sids=list(failures)) # We don't update the asset cache here because it should already be # updated by `self.retrieve_equities`. update_hits(self.retrieve_equities(type_to_assets.pop('equity', ()))) update_hits( self.retrieve_futures_contracts(type_to_assets.pop('future', ())) ) # We shouldn't know about any other asset types. if type_to_assets: raise AssertionError( "Found asset types: %s" % list(type_to_assets.keys()) ) return [hits[sid] for sid in sids]
def retrieve_all(self, sids, default_none=False): """ Retrieve all assets in `sids`. Parameters ---------- sids : iterable of int Assets to retrieve. default_none : bool If True, return None for failed lookups. If False, raise `SidsNotFound`. Returns ------- assets : list[Asset or None] A list of the same length as `sids` containing Assets (or Nones) corresponding to the requested sids. Raises ------ SidsNotFound When a requested sid is not found and default_none=False. """ sids = list(sids) hits, missing, failures = {}, set(), [] for sid in sids: try: asset = self._asset_cache[sid] if not default_none and asset is None: # Bail early if we've already cached that we don't know # about an asset. raise SidsNotFound(sids=[sid]) hits[sid] = asset except KeyError: missing.add(sid) # All requests were cache hits. Return requested sids in order. if not missing: return [hits[sid] for sid in sids] update_hits = hits.update # Look up cache misses by type. type_to_assets = self.group_by_type(missing) # Handle failures failures = {failure: None for failure in type_to_assets.pop(None, ())} update_hits(failures) self._asset_cache.update(failures) if failures and not default_none: raise SidsNotFound(sids=list(failures)) # We don't update the asset cache here because it should already be # updated by `self.retrieve_equities`. update_hits(self.retrieve_equities(type_to_assets.pop('equity', ()))) update_hits( self.retrieve_futures_contracts(type_to_assets.pop('future', ())) ) # We shouldn't know about any other asset types. if type_to_assets: raise AssertionError( "Found asset types: %s" % list(type_to_assets.keys()) ) return [hits[sid] for sid in sids]
[ "Retrieve", "all", "assets", "in", "sids", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L473-L539
[ "def", "retrieve_all", "(", "self", ",", "sids", ",", "default_none", "=", "False", ")", ":", "sids", "=", "list", "(", "sids", ")", "hits", ",", "missing", ",", "failures", "=", "{", "}", ",", "set", "(", ")", ",", "[", "]", "for", "sid", "in", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder._select_most_recent_symbols_chunk
Retrieve the most recent symbol for a set of sids. Parameters ---------- sid_group : iterable[int] The sids to lookup. The length of this sequence must be less than or equal to SQLITE_MAX_VARIABLE_NUMBER because the sids will be passed in as sql bind params. Returns ------- sel : Selectable The sqlalchemy selectable that will query for the most recent symbol for each sid. Notes ----- This is implemented as an inner select of the columns of interest ordered by the end date of the (sid, symbol) mapping. We then group that inner select on the sid with no aggregations to select the last row per group which gives us the most recently active symbol for all of the sids.
zipline/assets/assets.py
def _select_most_recent_symbols_chunk(self, sid_group): """Retrieve the most recent symbol for a set of sids. Parameters ---------- sid_group : iterable[int] The sids to lookup. The length of this sequence must be less than or equal to SQLITE_MAX_VARIABLE_NUMBER because the sids will be passed in as sql bind params. Returns ------- sel : Selectable The sqlalchemy selectable that will query for the most recent symbol for each sid. Notes ----- This is implemented as an inner select of the columns of interest ordered by the end date of the (sid, symbol) mapping. We then group that inner select on the sid with no aggregations to select the last row per group which gives us the most recently active symbol for all of the sids. """ cols = self.equity_symbol_mappings.c # These are the columns we actually want. data_cols = (cols.sid,) + tuple(cols[name] for name in symbol_columns) # Also select the max of end_date so that all non-grouped fields take # on the value associated with the max end_date. The SQLite docs say # this: # # When the min() or max() aggregate functions are used in an aggregate # query, all bare columns in the result set take values from the input # row which also contains the minimum or maximum. Only the built-in # min() and max() functions work this way. # # See https://www.sqlite.org/lang_select.html#resultset, for more info. to_select = data_cols + (sa.func.max(cols.end_date),) return sa.select( to_select, ).where( cols.sid.in_(map(int, sid_group)) ).group_by( cols.sid, )
def _select_most_recent_symbols_chunk(self, sid_group): """Retrieve the most recent symbol for a set of sids. Parameters ---------- sid_group : iterable[int] The sids to lookup. The length of this sequence must be less than or equal to SQLITE_MAX_VARIABLE_NUMBER because the sids will be passed in as sql bind params. Returns ------- sel : Selectable The sqlalchemy selectable that will query for the most recent symbol for each sid. Notes ----- This is implemented as an inner select of the columns of interest ordered by the end date of the (sid, symbol) mapping. We then group that inner select on the sid with no aggregations to select the last row per group which gives us the most recently active symbol for all of the sids. """ cols = self.equity_symbol_mappings.c # These are the columns we actually want. data_cols = (cols.sid,) + tuple(cols[name] for name in symbol_columns) # Also select the max of end_date so that all non-grouped fields take # on the value associated with the max end_date. The SQLite docs say # this: # # When the min() or max() aggregate functions are used in an aggregate # query, all bare columns in the result set take values from the input # row which also contains the minimum or maximum. Only the built-in # min() and max() functions work this way. # # See https://www.sqlite.org/lang_select.html#resultset, for more info. to_select = data_cols + (sa.func.max(cols.end_date),) return sa.select( to_select, ).where( cols.sid.in_(map(int, sid_group)) ).group_by( cols.sid, )
[ "Retrieve", "the", "most", "recent", "symbol", "for", "a", "set", "of", "sids", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L600-L647
[ "def", "_select_most_recent_symbols_chunk", "(", "self", ",", "sid_group", ")", ":", "cols", "=", "self", ".", "equity_symbol_mappings", ".", "c", "# These are the columns we actually want.", "data_cols", "=", "(", "cols", ".", "sid", ",", ")", "+", "tuple", "(", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder._retrieve_assets
Internal function for loading assets from a table. This should be the only method of `AssetFinder` that writes Assets into self._asset_cache. Parameters --------- sids : iterable of int Asset ids to look up. asset_tbl : sqlalchemy.Table Table from which to query assets. asset_type : type Type of asset to be constructed. Returns ------- assets : dict[int -> Asset] Dict mapping requested sids to the retrieved assets.
zipline/assets/assets.py
def _retrieve_assets(self, sids, asset_tbl, asset_type): """ Internal function for loading assets from a table. This should be the only method of `AssetFinder` that writes Assets into self._asset_cache. Parameters --------- sids : iterable of int Asset ids to look up. asset_tbl : sqlalchemy.Table Table from which to query assets. asset_type : type Type of asset to be constructed. Returns ------- assets : dict[int -> Asset] Dict mapping requested sids to the retrieved assets. """ # Fastpath for empty request. if not sids: return {} cache = self._asset_cache hits = {} querying_equities = issubclass(asset_type, Equity) filter_kwargs = ( _filter_equity_kwargs if querying_equities else _filter_future_kwargs ) rows = self._retrieve_asset_dicts(sids, asset_tbl, querying_equities) for row in rows: sid = row['sid'] asset = asset_type(**filter_kwargs(row)) hits[sid] = cache[sid] = asset # If we get here, it means something in our code thought that a # particular sid was an equity/future and called this function with a # concrete type, but we couldn't actually resolve the asset. This is # an error in our code, not a user-input error. misses = tuple(set(sids) - viewkeys(hits)) if misses: if querying_equities: raise EquitiesNotFound(sids=misses) else: raise FutureContractsNotFound(sids=misses) return hits
def _retrieve_assets(self, sids, asset_tbl, asset_type): """ Internal function for loading assets from a table. This should be the only method of `AssetFinder` that writes Assets into self._asset_cache. Parameters --------- sids : iterable of int Asset ids to look up. asset_tbl : sqlalchemy.Table Table from which to query assets. asset_type : type Type of asset to be constructed. Returns ------- assets : dict[int -> Asset] Dict mapping requested sids to the retrieved assets. """ # Fastpath for empty request. if not sids: return {} cache = self._asset_cache hits = {} querying_equities = issubclass(asset_type, Equity) filter_kwargs = ( _filter_equity_kwargs if querying_equities else _filter_future_kwargs ) rows = self._retrieve_asset_dicts(sids, asset_tbl, querying_equities) for row in rows: sid = row['sid'] asset = asset_type(**filter_kwargs(row)) hits[sid] = cache[sid] = asset # If we get here, it means something in our code thought that a # particular sid was an equity/future and called this function with a # concrete type, but we couldn't actually resolve the asset. This is # an error in our code, not a user-input error. misses = tuple(set(sids) - viewkeys(hits)) if misses: if querying_equities: raise EquitiesNotFound(sids=misses) else: raise FutureContractsNotFound(sids=misses) return hits
[ "Internal", "function", "for", "loading", "assets", "from", "a", "table", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L689-L740
[ "def", "_retrieve_assets", "(", "self", ",", "sids", ",", "asset_tbl", ",", "asset_type", ")", ":", "# Fastpath for empty request.", "if", "not", "sids", ":", "return", "{", "}", "cache", "=", "self", ".", "_asset_cache", "hits", "=", "{", "}", "querying_equ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder._lookup_symbol_strict
Resolve a symbol to an asset object without fuzzy matching. Parameters ---------- ownership_map : dict[(str, str), list[OwnershipPeriod]] The mapping from split symbols to ownership periods. multi_country : bool Does this mapping span multiple countries? symbol : str The symbol to look up. as_of_date : datetime or None If multiple assets have held this sid, which day should the resolution be checked against? If this value is None and multiple sids have held the ticker, then a MultipleSymbolsFound error will be raised. Returns ------- asset : Asset The asset that held the given symbol. Raises ------ SymbolNotFound Raised when the symbol or symbol as_of_date pair do not map to any assets. MultipleSymbolsFound Raised when multiple assets held the symbol. This happens if multiple assets held the symbol at disjoint times and ``as_of_date`` is None, or if multiple assets held the symbol at the same time and``multi_country`` is True. Notes ----- The resolution algorithm is as follows: - Split the symbol into the company and share class component. - Do a dictionary lookup of the ``(company_symbol, share_class_symbol)`` in the provided ownership map. - If there is no entry in the dictionary, we don't know about this symbol so raise a ``SymbolNotFound`` error. - If ``as_of_date`` is None: - If more there is more than one owner, raise ``MultipleSymbolsFound`` - Otherwise, because the list mapped to a symbol cannot be empty, return the single asset. - Iterate through all of the owners: - If the ``as_of_date`` is between the start and end of the ownership period: - If multi_country is False, return the found asset. - Otherwise, put the asset in a list. - At the end of the loop, if there are no candidate assets, raise a ``SymbolNotFound``. - If there is exactly one candidate, return it. - Othewise, raise ``MultipleSymbolsFound`` because the ticker is not unique across countries.
zipline/assets/assets.py
def _lookup_symbol_strict(self, ownership_map, multi_country, symbol, as_of_date): """ Resolve a symbol to an asset object without fuzzy matching. Parameters ---------- ownership_map : dict[(str, str), list[OwnershipPeriod]] The mapping from split symbols to ownership periods. multi_country : bool Does this mapping span multiple countries? symbol : str The symbol to look up. as_of_date : datetime or None If multiple assets have held this sid, which day should the resolution be checked against? If this value is None and multiple sids have held the ticker, then a MultipleSymbolsFound error will be raised. Returns ------- asset : Asset The asset that held the given symbol. Raises ------ SymbolNotFound Raised when the symbol or symbol as_of_date pair do not map to any assets. MultipleSymbolsFound Raised when multiple assets held the symbol. This happens if multiple assets held the symbol at disjoint times and ``as_of_date`` is None, or if multiple assets held the symbol at the same time and``multi_country`` is True. Notes ----- The resolution algorithm is as follows: - Split the symbol into the company and share class component. - Do a dictionary lookup of the ``(company_symbol, share_class_symbol)`` in the provided ownership map. - If there is no entry in the dictionary, we don't know about this symbol so raise a ``SymbolNotFound`` error. - If ``as_of_date`` is None: - If more there is more than one owner, raise ``MultipleSymbolsFound`` - Otherwise, because the list mapped to a symbol cannot be empty, return the single asset. - Iterate through all of the owners: - If the ``as_of_date`` is between the start and end of the ownership period: - If multi_country is False, return the found asset. - Otherwise, put the asset in a list. - At the end of the loop, if there are no candidate assets, raise a ``SymbolNotFound``. - If there is exactly one candidate, return it. - Othewise, raise ``MultipleSymbolsFound`` because the ticker is not unique across countries. """ # split the symbol into the components, if there are no # company/share class parts then share_class_symbol will be empty company_symbol, share_class_symbol = split_delimited_symbol(symbol) try: owners = ownership_map[company_symbol, share_class_symbol] assert owners, 'empty owners list for %r' % symbol except KeyError: # no equity has ever held this symbol raise SymbolNotFound(symbol=symbol) if not as_of_date: # exactly one equity has ever held this symbol, we may resolve # without the date if len(owners) == 1: return self.retrieve_asset(owners[0].sid) options = {self.retrieve_asset(owner.sid) for owner in owners} if multi_country: country_codes = map(attrgetter('country_code'), options) if len(set(country_codes)) > 1: raise SameSymbolUsedAcrossCountries( symbol=symbol, options=dict(zip(country_codes, options)) ) # more than one equity has held this ticker, this # is ambiguous without the date raise MultipleSymbolsFound(symbol=symbol, options=options) options = [] country_codes = [] for start, end, sid, _ in owners: if start <= as_of_date < end: # find the equity that owned it on the given asof date asset = self.retrieve_asset(sid) # if this asset owned the symbol on this asof date and we are # only searching one country, return that asset if not multi_country: return asset else: options.append(asset) country_codes.append(asset.country_code) if not options: # no equity held the ticker on the given asof date raise SymbolNotFound(symbol=symbol) # if there is one valid option given the asof date, return that option if len(options) == 1: return options[0] # if there's more than one option given the asof date, a country code # must be passed to resolve the symbol to an asset raise SameSymbolUsedAcrossCountries( symbol=symbol, options=dict(zip(country_codes, options)) )
def _lookup_symbol_strict(self, ownership_map, multi_country, symbol, as_of_date): """ Resolve a symbol to an asset object without fuzzy matching. Parameters ---------- ownership_map : dict[(str, str), list[OwnershipPeriod]] The mapping from split symbols to ownership periods. multi_country : bool Does this mapping span multiple countries? symbol : str The symbol to look up. as_of_date : datetime or None If multiple assets have held this sid, which day should the resolution be checked against? If this value is None and multiple sids have held the ticker, then a MultipleSymbolsFound error will be raised. Returns ------- asset : Asset The asset that held the given symbol. Raises ------ SymbolNotFound Raised when the symbol or symbol as_of_date pair do not map to any assets. MultipleSymbolsFound Raised when multiple assets held the symbol. This happens if multiple assets held the symbol at disjoint times and ``as_of_date`` is None, or if multiple assets held the symbol at the same time and``multi_country`` is True. Notes ----- The resolution algorithm is as follows: - Split the symbol into the company and share class component. - Do a dictionary lookup of the ``(company_symbol, share_class_symbol)`` in the provided ownership map. - If there is no entry in the dictionary, we don't know about this symbol so raise a ``SymbolNotFound`` error. - If ``as_of_date`` is None: - If more there is more than one owner, raise ``MultipleSymbolsFound`` - Otherwise, because the list mapped to a symbol cannot be empty, return the single asset. - Iterate through all of the owners: - If the ``as_of_date`` is between the start and end of the ownership period: - If multi_country is False, return the found asset. - Otherwise, put the asset in a list. - At the end of the loop, if there are no candidate assets, raise a ``SymbolNotFound``. - If there is exactly one candidate, return it. - Othewise, raise ``MultipleSymbolsFound`` because the ticker is not unique across countries. """ # split the symbol into the components, if there are no # company/share class parts then share_class_symbol will be empty company_symbol, share_class_symbol = split_delimited_symbol(symbol) try: owners = ownership_map[company_symbol, share_class_symbol] assert owners, 'empty owners list for %r' % symbol except KeyError: # no equity has ever held this symbol raise SymbolNotFound(symbol=symbol) if not as_of_date: # exactly one equity has ever held this symbol, we may resolve # without the date if len(owners) == 1: return self.retrieve_asset(owners[0].sid) options = {self.retrieve_asset(owner.sid) for owner in owners} if multi_country: country_codes = map(attrgetter('country_code'), options) if len(set(country_codes)) > 1: raise SameSymbolUsedAcrossCountries( symbol=symbol, options=dict(zip(country_codes, options)) ) # more than one equity has held this ticker, this # is ambiguous without the date raise MultipleSymbolsFound(symbol=symbol, options=options) options = [] country_codes = [] for start, end, sid, _ in owners: if start <= as_of_date < end: # find the equity that owned it on the given asof date asset = self.retrieve_asset(sid) # if this asset owned the symbol on this asof date and we are # only searching one country, return that asset if not multi_country: return asset else: options.append(asset) country_codes.append(asset.country_code) if not options: # no equity held the ticker on the given asof date raise SymbolNotFound(symbol=symbol) # if there is one valid option given the asof date, return that option if len(options) == 1: return options[0] # if there's more than one option given the asof date, a country code # must be passed to resolve the symbol to an asset raise SameSymbolUsedAcrossCountries( symbol=symbol, options=dict(zip(country_codes, options)) )
[ "Resolve", "a", "symbol", "to", "an", "asset", "object", "without", "fuzzy", "matching", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L742-L865
[ "def", "_lookup_symbol_strict", "(", "self", ",", "ownership_map", ",", "multi_country", ",", "symbol", ",", "as_of_date", ")", ":", "# split the symbol into the components, if there are no", "# company/share class parts then share_class_symbol will be empty", "company_symbol", ","...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder.lookup_symbol
Lookup an equity by symbol. Parameters ---------- symbol : str The ticker symbol to resolve. as_of_date : datetime or None Look up the last owner of this symbol as of this datetime. If ``as_of_date`` is None, then this can only resolve the equity if exactly one equity has ever owned the ticker. fuzzy : bool, optional Should fuzzy symbol matching be used? Fuzzy symbol matching attempts to resolve differences in representations for shareclasses. For example, some people may represent the ``A`` shareclass of ``BRK`` as ``BRK.A``, where others could write ``BRK_A``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equity : Equity The equity that held ``symbol`` on the given ``as_of_date``, or the only equity to hold ``symbol`` if ``as_of_date`` is None. Raises ------ SymbolNotFound Raised when no equity has ever held the given symbol. MultipleSymbolsFound Raised when no ``as_of_date`` is given and more than one equity has held ``symbol``. This is also raised when ``fuzzy=True`` and there are multiple candidates for the given ``symbol`` on the ``as_of_date``. Also raised when no ``country_code`` is given and the symbol is ambiguous across multiple countries.
zipline/assets/assets.py
def lookup_symbol(self, symbol, as_of_date, fuzzy=False, country_code=None): """Lookup an equity by symbol. Parameters ---------- symbol : str The ticker symbol to resolve. as_of_date : datetime or None Look up the last owner of this symbol as of this datetime. If ``as_of_date`` is None, then this can only resolve the equity if exactly one equity has ever owned the ticker. fuzzy : bool, optional Should fuzzy symbol matching be used? Fuzzy symbol matching attempts to resolve differences in representations for shareclasses. For example, some people may represent the ``A`` shareclass of ``BRK`` as ``BRK.A``, where others could write ``BRK_A``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equity : Equity The equity that held ``symbol`` on the given ``as_of_date``, or the only equity to hold ``symbol`` if ``as_of_date`` is None. Raises ------ SymbolNotFound Raised when no equity has ever held the given symbol. MultipleSymbolsFound Raised when no ``as_of_date`` is given and more than one equity has held ``symbol``. This is also raised when ``fuzzy=True`` and there are multiple candidates for the given ``symbol`` on the ``as_of_date``. Also raised when no ``country_code`` is given and the symbol is ambiguous across multiple countries. """ if symbol is None: raise TypeError("Cannot lookup asset for symbol of None for " "as of date %s." % as_of_date) if fuzzy: f = self._lookup_symbol_fuzzy mapping = self._choose_fuzzy_symbol_ownership_map(country_code) else: f = self._lookup_symbol_strict mapping = self._choose_symbol_ownership_map(country_code) if mapping is None: raise SymbolNotFound(symbol=symbol) return f( mapping, country_code is None, symbol, as_of_date, )
def lookup_symbol(self, symbol, as_of_date, fuzzy=False, country_code=None): """Lookup an equity by symbol. Parameters ---------- symbol : str The ticker symbol to resolve. as_of_date : datetime or None Look up the last owner of this symbol as of this datetime. If ``as_of_date`` is None, then this can only resolve the equity if exactly one equity has ever owned the ticker. fuzzy : bool, optional Should fuzzy symbol matching be used? Fuzzy symbol matching attempts to resolve differences in representations for shareclasses. For example, some people may represent the ``A`` shareclass of ``BRK`` as ``BRK.A``, where others could write ``BRK_A``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equity : Equity The equity that held ``symbol`` on the given ``as_of_date``, or the only equity to hold ``symbol`` if ``as_of_date`` is None. Raises ------ SymbolNotFound Raised when no equity has ever held the given symbol. MultipleSymbolsFound Raised when no ``as_of_date`` is given and more than one equity has held ``symbol``. This is also raised when ``fuzzy=True`` and there are multiple candidates for the given ``symbol`` on the ``as_of_date``. Also raised when no ``country_code`` is given and the symbol is ambiguous across multiple countries. """ if symbol is None: raise TypeError("Cannot lookup asset for symbol of None for " "as of date %s." % as_of_date) if fuzzy: f = self._lookup_symbol_fuzzy mapping = self._choose_fuzzy_symbol_ownership_map(country_code) else: f = self._lookup_symbol_strict mapping = self._choose_symbol_ownership_map(country_code) if mapping is None: raise SymbolNotFound(symbol=symbol) return f( mapping, country_code is None, symbol, as_of_date, )
[ "Lookup", "an", "equity", "by", "symbol", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L955-L1016
[ "def", "lookup_symbol", "(", "self", ",", "symbol", ",", "as_of_date", ",", "fuzzy", "=", "False", ",", "country_code", "=", "None", ")", ":", "if", "symbol", "is", "None", ":", "raise", "TypeError", "(", "\"Cannot lookup asset for symbol of None for \"", "\"as ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder.lookup_symbols
Lookup a list of equities by symbol. Equivalent to:: [finder.lookup_symbol(s, as_of, fuzzy) for s in symbols] but potentially faster because repeated lookups are memoized. Parameters ---------- symbols : sequence[str] Sequence of ticker symbols to resolve. as_of_date : pd.Timestamp Forwarded to ``lookup_symbol``. fuzzy : bool, optional Forwarded to ``lookup_symbol``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equities : list[Equity]
zipline/assets/assets.py
def lookup_symbols(self, symbols, as_of_date, fuzzy=False, country_code=None): """ Lookup a list of equities by symbol. Equivalent to:: [finder.lookup_symbol(s, as_of, fuzzy) for s in symbols] but potentially faster because repeated lookups are memoized. Parameters ---------- symbols : sequence[str] Sequence of ticker symbols to resolve. as_of_date : pd.Timestamp Forwarded to ``lookup_symbol``. fuzzy : bool, optional Forwarded to ``lookup_symbol``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equities : list[Equity] """ if not symbols: return [] multi_country = country_code is None if fuzzy: f = self._lookup_symbol_fuzzy mapping = self._choose_fuzzy_symbol_ownership_map(country_code) else: f = self._lookup_symbol_strict mapping = self._choose_symbol_ownership_map(country_code) if mapping is None: raise SymbolNotFound(symbol=symbols[0]) memo = {} out = [] append_output = out.append for sym in symbols: if sym in memo: append_output(memo[sym]) else: equity = memo[sym] = f( mapping, multi_country, sym, as_of_date, ) append_output(equity) return out
def lookup_symbols(self, symbols, as_of_date, fuzzy=False, country_code=None): """ Lookup a list of equities by symbol. Equivalent to:: [finder.lookup_symbol(s, as_of, fuzzy) for s in symbols] but potentially faster because repeated lookups are memoized. Parameters ---------- symbols : sequence[str] Sequence of ticker symbols to resolve. as_of_date : pd.Timestamp Forwarded to ``lookup_symbol``. fuzzy : bool, optional Forwarded to ``lookup_symbol``. country_code : str or None, optional The country to limit searches to. If not provided, the search will span all countries which increases the likelihood of an ambiguous lookup. Returns ------- equities : list[Equity] """ if not symbols: return [] multi_country = country_code is None if fuzzy: f = self._lookup_symbol_fuzzy mapping = self._choose_fuzzy_symbol_ownership_map(country_code) else: f = self._lookup_symbol_strict mapping = self._choose_symbol_ownership_map(country_code) if mapping is None: raise SymbolNotFound(symbol=symbols[0]) memo = {} out = [] append_output = out.append for sym in symbols: if sym in memo: append_output(memo[sym]) else: equity = memo[sym] = f( mapping, multi_country, sym, as_of_date, ) append_output(equity) return out
[ "Lookup", "a", "list", "of", "equities", "by", "symbol", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L1018-L1077
[ "def", "lookup_symbols", "(", "self", ",", "symbols", ",", "as_of_date", ",", "fuzzy", "=", "False", ",", "country_code", "=", "None", ")", ":", "if", "not", "symbols", ":", "return", "[", "]", "multi_country", "=", "country_code", "is", "None", "if", "f...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder.lookup_future_symbol
Lookup a future contract by symbol. Parameters ---------- symbol : str The symbol of the desired contract. Returns ------- future : Future The future contract referenced by ``symbol``. Raises ------ SymbolNotFound Raised when no contract named 'symbol' is found.
zipline/assets/assets.py
def lookup_future_symbol(self, symbol): """Lookup a future contract by symbol. Parameters ---------- symbol : str The symbol of the desired contract. Returns ------- future : Future The future contract referenced by ``symbol``. Raises ------ SymbolNotFound Raised when no contract named 'symbol' is found. """ data = self._select_asset_by_symbol(self.futures_contracts, symbol)\ .execute().fetchone() # If no data found, raise an exception if not data: raise SymbolNotFound(symbol=symbol) return self.retrieve_asset(data['sid'])
def lookup_future_symbol(self, symbol): """Lookup a future contract by symbol. Parameters ---------- symbol : str The symbol of the desired contract. Returns ------- future : Future The future contract referenced by ``symbol``. Raises ------ SymbolNotFound Raised when no contract named 'symbol' is found. """ data = self._select_asset_by_symbol(self.futures_contracts, symbol)\ .execute().fetchone() # If no data found, raise an exception if not data: raise SymbolNotFound(symbol=symbol) return self.retrieve_asset(data['sid'])
[ "Lookup", "a", "future", "contract", "by", "symbol", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L1079-L1105
[ "def", "lookup_future_symbol", "(", "self", ",", "symbol", ")", ":", "data", "=", "self", ".", "_select_asset_by_symbol", "(", "self", ".", "futures_contracts", ",", "symbol", ")", ".", "execute", "(", ")", ".", "fetchone", "(", ")", "# If no data found, raise...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder.get_supplementary_field
Get the value of a supplementary field for an asset. Parameters ---------- sid : int The sid of the asset to query. field_name : str Name of the supplementary field. as_of_date : pd.Timestamp, None The last known value on this date is returned. If None, a value is returned only if we've only ever had one value for this sid. If None and we've had multiple values, MultipleValuesFoundForSid is raised. Raises ------ NoValueForSid If we have no values for this asset, or no values was known on this as_of_date. MultipleValuesFoundForSid If we have had multiple values for this asset over time, and None was passed for as_of_date.
zipline/assets/assets.py
def get_supplementary_field(self, sid, field_name, as_of_date): """Get the value of a supplementary field for an asset. Parameters ---------- sid : int The sid of the asset to query. field_name : str Name of the supplementary field. as_of_date : pd.Timestamp, None The last known value on this date is returned. If None, a value is returned only if we've only ever had one value for this sid. If None and we've had multiple values, MultipleValuesFoundForSid is raised. Raises ------ NoValueForSid If we have no values for this asset, or no values was known on this as_of_date. MultipleValuesFoundForSid If we have had multiple values for this asset over time, and None was passed for as_of_date. """ try: periods = self.equity_supplementary_map_by_sid[ field_name, sid, ] assert periods, 'empty periods list for %r' % (field_name, sid) except KeyError: raise NoValueForSid(field=field_name, sid=sid) if not as_of_date: if len(periods) > 1: # This equity has held more than one value, this is ambigious # without the date raise MultipleValuesFoundForSid( field=field_name, sid=sid, options={p.value for p in periods}, ) # this equity has only ever held this value, we may resolve # without the date return periods[0].value for start, end, _, value in periods: if start <= as_of_date < end: return value # Could not find a value for this sid on the as_of_date. raise NoValueForSid(field=field_name, sid=sid)
def get_supplementary_field(self, sid, field_name, as_of_date): """Get the value of a supplementary field for an asset. Parameters ---------- sid : int The sid of the asset to query. field_name : str Name of the supplementary field. as_of_date : pd.Timestamp, None The last known value on this date is returned. If None, a value is returned only if we've only ever had one value for this sid. If None and we've had multiple values, MultipleValuesFoundForSid is raised. Raises ------ NoValueForSid If we have no values for this asset, or no values was known on this as_of_date. MultipleValuesFoundForSid If we have had multiple values for this asset over time, and None was passed for as_of_date. """ try: periods = self.equity_supplementary_map_by_sid[ field_name, sid, ] assert periods, 'empty periods list for %r' % (field_name, sid) except KeyError: raise NoValueForSid(field=field_name, sid=sid) if not as_of_date: if len(periods) > 1: # This equity has held more than one value, this is ambigious # without the date raise MultipleValuesFoundForSid( field=field_name, sid=sid, options={p.value for p in periods}, ) # this equity has only ever held this value, we may resolve # without the date return periods[0].value for start, end, _, value in periods: if start <= as_of_date < end: return value # Could not find a value for this sid on the as_of_date. raise NoValueForSid(field=field_name, sid=sid)
[ "Get", "the", "value", "of", "a", "supplementary", "field", "for", "an", "asset", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L1142-L1193
[ "def", "get_supplementary_field", "(", "self", ",", "sid", ",", "field_name", ",", "as_of_date", ")", ":", "try", ":", "periods", "=", "self", ".", "equity_supplementary_map_by_sid", "[", "field_name", ",", "sid", ",", "]", "assert", "periods", ",", "'empty pe...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder._lookup_generic_scalar
Convert asset_convertible to an asset. On success, append to matches. On failure, append to missing.
zipline/assets/assets.py
def _lookup_generic_scalar(self, obj, as_of_date, country_code, matches, missing): """ Convert asset_convertible to an asset. On success, append to matches. On failure, append to missing. """ result = self._lookup_generic_scalar_helper( obj, as_of_date, country_code, ) if result is not None: matches.append(result) else: missing.append(obj)
def _lookup_generic_scalar(self, obj, as_of_date, country_code, matches, missing): """ Convert asset_convertible to an asset. On success, append to matches. On failure, append to missing. """ result = self._lookup_generic_scalar_helper( obj, as_of_date, country_code, ) if result is not None: matches.append(result) else: missing.append(obj)
[ "Convert", "asset_convertible", "to", "an", "asset", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L1298-L1316
[ "def", "_lookup_generic_scalar", "(", "self", ",", "obj", ",", "as_of_date", ",", "country_code", ",", "matches", ",", "missing", ")", ":", "result", "=", "self", ".", "_lookup_generic_scalar_helper", "(", "obj", ",", "as_of_date", ",", "country_code", ",", ")...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder.lookup_generic
Convert an object into an Asset or sequence of Assets. This method exists primarily as a convenience for implementing user-facing APIs that can handle multiple kinds of input. It should not be used for internal code where we already know the expected types of our inputs. Parameters ---------- obj : int, str, Asset, ContinuousFuture, or iterable The object to be converted into one or more Assets. Integers are interpreted as sids. Strings are interpreted as tickers. Assets and ContinuousFutures are returned unchanged. as_of_date : pd.Timestamp or None Timestamp to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. country_code : str or None ISO-3166 country code to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. Returns ------- matches, missing : tuple ``matches`` is the result of the conversion. ``missing`` is a list containing any values that couldn't be resolved. If ``obj`` is not an iterable, ``missing`` will be an empty list.
zipline/assets/assets.py
def lookup_generic(self, obj, as_of_date, country_code): """ Convert an object into an Asset or sequence of Assets. This method exists primarily as a convenience for implementing user-facing APIs that can handle multiple kinds of input. It should not be used for internal code where we already know the expected types of our inputs. Parameters ---------- obj : int, str, Asset, ContinuousFuture, or iterable The object to be converted into one or more Assets. Integers are interpreted as sids. Strings are interpreted as tickers. Assets and ContinuousFutures are returned unchanged. as_of_date : pd.Timestamp or None Timestamp to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. country_code : str or None ISO-3166 country code to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. Returns ------- matches, missing : tuple ``matches`` is the result of the conversion. ``missing`` is a list containing any values that couldn't be resolved. If ``obj`` is not an iterable, ``missing`` will be an empty list. """ matches = [] missing = [] # Interpret input as scalar. if isinstance(obj, (AssetConvertible, ContinuousFuture)): self._lookup_generic_scalar( obj=obj, as_of_date=as_of_date, country_code=country_code, matches=matches, missing=missing, ) try: return matches[0], missing except IndexError: if hasattr(obj, '__int__'): raise SidsNotFound(sids=[obj]) else: raise SymbolNotFound(symbol=obj) # Interpret input as iterable. try: iterator = iter(obj) except TypeError: raise NotAssetConvertible( "Input was not a AssetConvertible " "or iterable of AssetConvertible." ) for obj in iterator: self._lookup_generic_scalar( obj=obj, as_of_date=as_of_date, country_code=country_code, matches=matches, missing=missing, ) return matches, missing
def lookup_generic(self, obj, as_of_date, country_code): """ Convert an object into an Asset or sequence of Assets. This method exists primarily as a convenience for implementing user-facing APIs that can handle multiple kinds of input. It should not be used for internal code where we already know the expected types of our inputs. Parameters ---------- obj : int, str, Asset, ContinuousFuture, or iterable The object to be converted into one or more Assets. Integers are interpreted as sids. Strings are interpreted as tickers. Assets and ContinuousFutures are returned unchanged. as_of_date : pd.Timestamp or None Timestamp to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. country_code : str or None ISO-3166 country code to use to disambiguate ticker lookups. Has the same semantics as in `lookup_symbol`. Returns ------- matches, missing : tuple ``matches`` is the result of the conversion. ``missing`` is a list containing any values that couldn't be resolved. If ``obj`` is not an iterable, ``missing`` will be an empty list. """ matches = [] missing = [] # Interpret input as scalar. if isinstance(obj, (AssetConvertible, ContinuousFuture)): self._lookup_generic_scalar( obj=obj, as_of_date=as_of_date, country_code=country_code, matches=matches, missing=missing, ) try: return matches[0], missing except IndexError: if hasattr(obj, '__int__'): raise SidsNotFound(sids=[obj]) else: raise SymbolNotFound(symbol=obj) # Interpret input as iterable. try: iterator = iter(obj) except TypeError: raise NotAssetConvertible( "Input was not a AssetConvertible " "or iterable of AssetConvertible." ) for obj in iterator: self._lookup_generic_scalar( obj=obj, as_of_date=as_of_date, country_code=country_code, matches=matches, missing=missing, ) return matches, missing
[ "Convert", "an", "object", "into", "an", "Asset", "or", "sequence", "of", "Assets", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L1347-L1414
[ "def", "lookup_generic", "(", "self", ",", "obj", ",", "as_of_date", ",", "country_code", ")", ":", "matches", "=", "[", "]", "missing", "=", "[", "]", "# Interpret input as scalar.", "if", "isinstance", "(", "obj", ",", "(", "AssetConvertible", ",", "Contin...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder._compute_asset_lifetimes
Compute and cache a recarray of asset lifetimes.
zipline/assets/assets.py
def _compute_asset_lifetimes(self, country_codes): """ Compute and cache a recarray of asset lifetimes. """ equities_cols = self.equities.c if country_codes: buf = np.array( tuple( sa.select(( equities_cols.sid, equities_cols.start_date, equities_cols.end_date, )).where( (self.exchanges.c.exchange == equities_cols.exchange) & (self.exchanges.c.country_code.in_(country_codes)) ).execute(), ), dtype='f8', # use doubles so we get NaNs ) else: buf = np.array([], dtype='f8') lifetimes = np.recarray( buf=buf, shape=(len(buf),), dtype=[ ('sid', 'f8'), ('start', 'f8'), ('end', 'f8') ], ) start = lifetimes.start end = lifetimes.end start[np.isnan(start)] = 0 # convert missing starts to 0 end[np.isnan(end)] = np.iinfo(int).max # convert missing end to INTMAX # Cast the results back down to int. return lifetimes.astype([ ('sid', 'i8'), ('start', 'i8'), ('end', 'i8'), ])
def _compute_asset_lifetimes(self, country_codes): """ Compute and cache a recarray of asset lifetimes. """ equities_cols = self.equities.c if country_codes: buf = np.array( tuple( sa.select(( equities_cols.sid, equities_cols.start_date, equities_cols.end_date, )).where( (self.exchanges.c.exchange == equities_cols.exchange) & (self.exchanges.c.country_code.in_(country_codes)) ).execute(), ), dtype='f8', # use doubles so we get NaNs ) else: buf = np.array([], dtype='f8') lifetimes = np.recarray( buf=buf, shape=(len(buf),), dtype=[ ('sid', 'f8'), ('start', 'f8'), ('end', 'f8') ], ) start = lifetimes.start end = lifetimes.end start[np.isnan(start)] = 0 # convert missing starts to 0 end[np.isnan(end)] = np.iinfo(int).max # convert missing end to INTMAX # Cast the results back down to int. return lifetimes.astype([ ('sid', 'i8'), ('start', 'i8'), ('end', 'i8'), ])
[ "Compute", "and", "cache", "a", "recarray", "of", "asset", "lifetimes", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L1416-L1456
[ "def", "_compute_asset_lifetimes", "(", "self", ",", "country_codes", ")", ":", "equities_cols", "=", "self", ".", "equities", ".", "c", "if", "country_codes", ":", "buf", "=", "np", ".", "array", "(", "tuple", "(", "sa", ".", "select", "(", "(", "equiti...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder.lifetimes
Compute a DataFrame representing asset lifetimes for the specified date range. Parameters ---------- dates : pd.DatetimeIndex The dates for which to compute lifetimes. include_start_date : bool Whether or not to count the asset as alive on its start_date. This is useful in a backtesting context where `lifetimes` is being used to signify "do I have data for this asset as of the morning of this date?" For many financial metrics, (e.g. daily close), data isn't available for an asset until the end of the asset's first day. country_codes : iterable[str] The country codes to get lifetimes for. Returns ------- lifetimes : pd.DataFrame A frame of dtype bool with `dates` as index and an Int64Index of assets as columns. The value at `lifetimes.loc[date, asset]` will be True iff `asset` existed on `date`. If `include_start_date` is False, then lifetimes.loc[date, asset] will be false when date == asset.start_date. See Also -------- numpy.putmask zipline.pipeline.engine.SimplePipelineEngine._compute_root_mask
zipline/assets/assets.py
def lifetimes(self, dates, include_start_date, country_codes): """ Compute a DataFrame representing asset lifetimes for the specified date range. Parameters ---------- dates : pd.DatetimeIndex The dates for which to compute lifetimes. include_start_date : bool Whether or not to count the asset as alive on its start_date. This is useful in a backtesting context where `lifetimes` is being used to signify "do I have data for this asset as of the morning of this date?" For many financial metrics, (e.g. daily close), data isn't available for an asset until the end of the asset's first day. country_codes : iterable[str] The country codes to get lifetimes for. Returns ------- lifetimes : pd.DataFrame A frame of dtype bool with `dates` as index and an Int64Index of assets as columns. The value at `lifetimes.loc[date, asset]` will be True iff `asset` existed on `date`. If `include_start_date` is False, then lifetimes.loc[date, asset] will be false when date == asset.start_date. See Also -------- numpy.putmask zipline.pipeline.engine.SimplePipelineEngine._compute_root_mask """ if isinstance(country_codes, string_types): raise TypeError( "Got string {!r} instead of an iterable of strings in " "AssetFinder.lifetimes.".format(country_codes), ) # normalize to a cache-key so that we can memoize results. country_codes = frozenset(country_codes) lifetimes = self._asset_lifetimes.get(country_codes) if lifetimes is None: self._asset_lifetimes[country_codes] = lifetimes = ( self._compute_asset_lifetimes(country_codes) ) raw_dates = as_column(dates.asi8) if include_start_date: mask = lifetimes.start <= raw_dates else: mask = lifetimes.start < raw_dates mask &= (raw_dates <= lifetimes.end) return pd.DataFrame(mask, index=dates, columns=lifetimes.sid)
def lifetimes(self, dates, include_start_date, country_codes): """ Compute a DataFrame representing asset lifetimes for the specified date range. Parameters ---------- dates : pd.DatetimeIndex The dates for which to compute lifetimes. include_start_date : bool Whether or not to count the asset as alive on its start_date. This is useful in a backtesting context where `lifetimes` is being used to signify "do I have data for this asset as of the morning of this date?" For many financial metrics, (e.g. daily close), data isn't available for an asset until the end of the asset's first day. country_codes : iterable[str] The country codes to get lifetimes for. Returns ------- lifetimes : pd.DataFrame A frame of dtype bool with `dates` as index and an Int64Index of assets as columns. The value at `lifetimes.loc[date, asset]` will be True iff `asset` existed on `date`. If `include_start_date` is False, then lifetimes.loc[date, asset] will be false when date == asset.start_date. See Also -------- numpy.putmask zipline.pipeline.engine.SimplePipelineEngine._compute_root_mask """ if isinstance(country_codes, string_types): raise TypeError( "Got string {!r} instead of an iterable of strings in " "AssetFinder.lifetimes.".format(country_codes), ) # normalize to a cache-key so that we can memoize results. country_codes = frozenset(country_codes) lifetimes = self._asset_lifetimes.get(country_codes) if lifetimes is None: self._asset_lifetimes[country_codes] = lifetimes = ( self._compute_asset_lifetimes(country_codes) ) raw_dates = as_column(dates.asi8) if include_start_date: mask = lifetimes.start <= raw_dates else: mask = lifetimes.start < raw_dates mask &= (raw_dates <= lifetimes.end) return pd.DataFrame(mask, index=dates, columns=lifetimes.sid)
[ "Compute", "a", "DataFrame", "representing", "asset", "lifetimes", "for", "the", "specified", "date", "range", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L1458-L1514
[ "def", "lifetimes", "(", "self", ",", "dates", ",", "include_start_date", ",", "country_codes", ")", ":", "if", "isinstance", "(", "country_codes", ",", "string_types", ")", ":", "raise", "TypeError", "(", "\"Got string {!r} instead of an iterable of strings in \"", "...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
AssetFinder.equities_sids_for_country_code
Return all of the sids for a given country. Parameters ---------- country_code : str An ISO 3166 alpha-2 country code. Returns ------- tuple[int] The sids whose exchanges are in this country.
zipline/assets/assets.py
def equities_sids_for_country_code(self, country_code): """Return all of the sids for a given country. Parameters ---------- country_code : str An ISO 3166 alpha-2 country code. Returns ------- tuple[int] The sids whose exchanges are in this country. """ sids = self._compute_asset_lifetimes([country_code]).sid return tuple(sids.tolist())
def equities_sids_for_country_code(self, country_code): """Return all of the sids for a given country. Parameters ---------- country_code : str An ISO 3166 alpha-2 country code. Returns ------- tuple[int] The sids whose exchanges are in this country. """ sids = self._compute_asset_lifetimes([country_code]).sid return tuple(sids.tolist())
[ "Return", "all", "of", "the", "sids", "for", "a", "given", "country", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/assets/assets.py#L1516-L1530
[ "def", "equities_sids_for_country_code", "(", "self", ",", "country_code", ")", ":", "sids", "=", "self", ".", "_compute_asset_lifetimes", "(", "[", "country_code", "]", ")", ".", "sid", "return", "tuple", "(", "sids", ".", "tolist", "(", ")", ")" ]
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
ContinuousFutureSessionBarReader.load_raw_arrays
Parameters ---------- fields : list of str 'sid' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range.
zipline/data/continuous_future_reader.py
def load_raw_arrays(self, columns, start_date, end_date, assets): """ Parameters ---------- fields : list of str 'sid' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range. """ rolls_by_asset = {} for asset in assets: rf = self._roll_finders[asset.roll_style] rolls_by_asset[asset] = rf.get_rolls( asset.root_symbol, start_date, end_date, asset.offset ) num_sessions = len( self.trading_calendar.sessions_in_range(start_date, end_date) ) shape = num_sessions, len(assets) results = [] tc = self._bar_reader.trading_calendar sessions = tc.sessions_in_range(start_date, end_date) # Get partitions partitions_by_asset = {} for asset in assets: partitions = [] partitions_by_asset[asset] = partitions rolls = rolls_by_asset[asset] start = start_date for roll in rolls: sid, roll_date = roll start_loc = sessions.get_loc(start) if roll_date is not None: end = roll_date - sessions.freq end_loc = sessions.get_loc(end) else: end = end_date end_loc = len(sessions) - 1 partitions.append((sid, start, end, start_loc, end_loc)) if roll_date is not None: start = sessions[end_loc + 1] for column in columns: if column != 'volume' and column != 'sid': out = np.full(shape, np.nan) else: out = np.zeros(shape, dtype=np.int64) for i, asset in enumerate(assets): partitions = partitions_by_asset[asset] for sid, start, end, start_loc, end_loc in partitions: if column != 'sid': result = self._bar_reader.load_raw_arrays( [column], start, end, [sid])[0][:, 0] else: result = int(sid) out[start_loc:end_loc + 1, i] = result results.append(out) return results
def load_raw_arrays(self, columns, start_date, end_date, assets): """ Parameters ---------- fields : list of str 'sid' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range. """ rolls_by_asset = {} for asset in assets: rf = self._roll_finders[asset.roll_style] rolls_by_asset[asset] = rf.get_rolls( asset.root_symbol, start_date, end_date, asset.offset ) num_sessions = len( self.trading_calendar.sessions_in_range(start_date, end_date) ) shape = num_sessions, len(assets) results = [] tc = self._bar_reader.trading_calendar sessions = tc.sessions_in_range(start_date, end_date) # Get partitions partitions_by_asset = {} for asset in assets: partitions = [] partitions_by_asset[asset] = partitions rolls = rolls_by_asset[asset] start = start_date for roll in rolls: sid, roll_date = roll start_loc = sessions.get_loc(start) if roll_date is not None: end = roll_date - sessions.freq end_loc = sessions.get_loc(end) else: end = end_date end_loc = len(sessions) - 1 partitions.append((sid, start, end, start_loc, end_loc)) if roll_date is not None: start = sessions[end_loc + 1] for column in columns: if column != 'volume' and column != 'sid': out = np.full(shape, np.nan) else: out = np.zeros(shape, dtype=np.int64) for i, asset in enumerate(assets): partitions = partitions_by_asset[asset] for sid, start, end, start_loc, end_loc in partitions: if column != 'sid': result = self._bar_reader.load_raw_arrays( [column], start, end, [sid])[0][:, 0] else: result = int(sid) out[start_loc:end_loc + 1, i] = result results.append(out) return results
[ "Parameters", "----------", "fields", ":", "list", "of", "str", "sid", "start_dt", ":", "Timestamp", "Beginning", "of", "the", "window", "range", ".", "end_dt", ":", "Timestamp", "End", "of", "the", "window", "range", ".", "sids", ":", "list", "of", "int",...
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/continuous_future_reader.py#L12-L96
[ "def", "load_raw_arrays", "(", "self", ",", "columns", ",", "start_date", ",", "end_date", ",", "assets", ")", ":", "rolls_by_asset", "=", "{", "}", "for", "asset", "in", "assets", ":", "rf", "=", "self", ".", "_roll_finders", "[", "asset", ".", "roll_st...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
ContinuousFutureSessionBarReader.get_value
Retrieve the value at the given coordinates. Parameters ---------- sid : int The asset identifier. dt : pd.Timestamp The timestamp for the desired data point. field : string The OHLVC name for the desired data point. Returns ------- value : float|int The value at the given coordinates, ``float`` for OHLC, ``int`` for 'volume'. Raises ------ NoDataOnDate If the given dt is not a valid market minute (in minute mode) or session (in daily mode) according to this reader's tradingcalendar.
zipline/data/continuous_future_reader.py
def get_value(self, continuous_future, dt, field): """ Retrieve the value at the given coordinates. Parameters ---------- sid : int The asset identifier. dt : pd.Timestamp The timestamp for the desired data point. field : string The OHLVC name for the desired data point. Returns ------- value : float|int The value at the given coordinates, ``float`` for OHLC, ``int`` for 'volume'. Raises ------ NoDataOnDate If the given dt is not a valid market minute (in minute mode) or session (in daily mode) according to this reader's tradingcalendar. """ rf = self._roll_finders[continuous_future.roll_style] sid = (rf.get_contract_center(continuous_future.root_symbol, dt, continuous_future.offset)) return self._bar_reader.get_value(sid, dt, field)
def get_value(self, continuous_future, dt, field): """ Retrieve the value at the given coordinates. Parameters ---------- sid : int The asset identifier. dt : pd.Timestamp The timestamp for the desired data point. field : string The OHLVC name for the desired data point. Returns ------- value : float|int The value at the given coordinates, ``float`` for OHLC, ``int`` for 'volume'. Raises ------ NoDataOnDate If the given dt is not a valid market minute (in minute mode) or session (in daily mode) according to this reader's tradingcalendar. """ rf = self._roll_finders[continuous_future.roll_style] sid = (rf.get_contract_center(continuous_future.root_symbol, dt, continuous_future.offset)) return self._bar_reader.get_value(sid, dt, field)
[ "Retrieve", "the", "value", "at", "the", "given", "coordinates", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/continuous_future_reader.py#L127-L156
[ "def", "get_value", "(", "self", ",", "continuous_future", ",", "dt", ",", "field", ")", ":", "rf", "=", "self", ".", "_roll_finders", "[", "continuous_future", ".", "roll_style", "]", "sid", "=", "(", "rf", ".", "get_contract_center", "(", "continuous_futur...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
ContinuousFutureSessionBarReader.get_last_traded_dt
Get the latest minute on or before ``dt`` in which ``asset`` traded. If there are no trades on or before ``dt``, returns ``pd.NaT``. Parameters ---------- asset : zipline.asset.Asset The asset for which to get the last traded minute. dt : pd.Timestamp The minute at which to start searching for the last traded minute. Returns ------- last_traded : pd.Timestamp The dt of the last trade for the given asset, using the input dt as a vantage point.
zipline/data/continuous_future_reader.py
def get_last_traded_dt(self, asset, dt): """ Get the latest minute on or before ``dt`` in which ``asset`` traded. If there are no trades on or before ``dt``, returns ``pd.NaT``. Parameters ---------- asset : zipline.asset.Asset The asset for which to get the last traded minute. dt : pd.Timestamp The minute at which to start searching for the last traded minute. Returns ------- last_traded : pd.Timestamp The dt of the last trade for the given asset, using the input dt as a vantage point. """ rf = self._roll_finders[asset.roll_style] sid = (rf.get_contract_center(asset.root_symbol, dt, asset.offset)) if sid is None: return pd.NaT contract = rf.asset_finder.retrieve_asset(sid) return self._bar_reader.get_last_traded_dt(contract, dt)
def get_last_traded_dt(self, asset, dt): """ Get the latest minute on or before ``dt`` in which ``asset`` traded. If there are no trades on or before ``dt``, returns ``pd.NaT``. Parameters ---------- asset : zipline.asset.Asset The asset for which to get the last traded minute. dt : pd.Timestamp The minute at which to start searching for the last traded minute. Returns ------- last_traded : pd.Timestamp The dt of the last trade for the given asset, using the input dt as a vantage point. """ rf = self._roll_finders[asset.roll_style] sid = (rf.get_contract_center(asset.root_symbol, dt, asset.offset)) if sid is None: return pd.NaT contract = rf.asset_finder.retrieve_asset(sid) return self._bar_reader.get_last_traded_dt(contract, dt)
[ "Get", "the", "latest", "minute", "on", "or", "before", "dt", "in", "which", "asset", "traded", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/continuous_future_reader.py#L158-L184
[ "def", "get_last_traded_dt", "(", "self", ",", "asset", ",", "dt", ")", ":", "rf", "=", "self", ".", "_roll_finders", "[", "asset", ".", "roll_style", "]", "sid", "=", "(", "rf", ".", "get_contract_center", "(", "asset", ".", "root_symbol", ",", "dt", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
ContinuousFutureMinuteBarReader.load_raw_arrays
Parameters ---------- fields : list of str 'open', 'high', 'low', 'close', or 'volume' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range.
zipline/data/continuous_future_reader.py
def load_raw_arrays(self, columns, start_date, end_date, assets): """ Parameters ---------- fields : list of str 'open', 'high', 'low', 'close', or 'volume' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range. """ rolls_by_asset = {} tc = self.trading_calendar start_session = tc.minute_to_session_label(start_date) end_session = tc.minute_to_session_label(end_date) for asset in assets: rf = self._roll_finders[asset.roll_style] rolls_by_asset[asset] = rf.get_rolls( asset.root_symbol, start_session, end_session, asset.offset) sessions = tc.sessions_in_range(start_date, end_date) minutes = tc.minutes_in_range(start_date, end_date) num_minutes = len(minutes) shape = num_minutes, len(assets) results = [] # Get partitions partitions_by_asset = {} for asset in assets: partitions = [] partitions_by_asset[asset] = partitions rolls = rolls_by_asset[asset] start = start_date for roll in rolls: sid, roll_date = roll start_loc = minutes.searchsorted(start) if roll_date is not None: _, end = tc.open_and_close_for_session( roll_date - sessions.freq) end_loc = minutes.searchsorted(end) else: end = end_date end_loc = len(minutes) - 1 partitions.append((sid, start, end, start_loc, end_loc)) if roll[-1] is not None: start, _ = tc.open_and_close_for_session( tc.minute_to_session_label(minutes[end_loc + 1])) for column in columns: if column != 'volume': out = np.full(shape, np.nan) else: out = np.zeros(shape, dtype=np.uint32) for i, asset in enumerate(assets): partitions = partitions_by_asset[asset] for sid, start, end, start_loc, end_loc in partitions: if column != 'sid': result = self._bar_reader.load_raw_arrays( [column], start, end, [sid])[0][:, 0] else: result = int(sid) out[start_loc:end_loc + 1, i] = result results.append(out) return results
def load_raw_arrays(self, columns, start_date, end_date, assets): """ Parameters ---------- fields : list of str 'open', 'high', 'low', 'close', or 'volume' start_dt: Timestamp Beginning of the window range. end_dt: Timestamp End of the window range. sids : list of int The asset identifiers in the window. Returns ------- list of np.ndarray A list with an entry per field of ndarrays with shape (minutes in range, sids) with a dtype of float64, containing the values for the respective field over start and end dt range. """ rolls_by_asset = {} tc = self.trading_calendar start_session = tc.minute_to_session_label(start_date) end_session = tc.minute_to_session_label(end_date) for asset in assets: rf = self._roll_finders[asset.roll_style] rolls_by_asset[asset] = rf.get_rolls( asset.root_symbol, start_session, end_session, asset.offset) sessions = tc.sessions_in_range(start_date, end_date) minutes = tc.minutes_in_range(start_date, end_date) num_minutes = len(minutes) shape = num_minutes, len(assets) results = [] # Get partitions partitions_by_asset = {} for asset in assets: partitions = [] partitions_by_asset[asset] = partitions rolls = rolls_by_asset[asset] start = start_date for roll in rolls: sid, roll_date = roll start_loc = minutes.searchsorted(start) if roll_date is not None: _, end = tc.open_and_close_for_session( roll_date - sessions.freq) end_loc = minutes.searchsorted(end) else: end = end_date end_loc = len(minutes) - 1 partitions.append((sid, start, end, start_loc, end_loc)) if roll[-1] is not None: start, _ = tc.open_and_close_for_session( tc.minute_to_session_label(minutes[end_loc + 1])) for column in columns: if column != 'volume': out = np.full(shape, np.nan) else: out = np.zeros(shape, dtype=np.uint32) for i, asset in enumerate(assets): partitions = partitions_by_asset[asset] for sid, start, end, start_loc, end_loc in partitions: if column != 'sid': result = self._bar_reader.load_raw_arrays( [column], start, end, [sid])[0][:, 0] else: result = int(sid) out[start_loc:end_loc + 1, i] = result results.append(out) return results
[ "Parameters", "----------", "fields", ":", "list", "of", "str", "open", "high", "low", "close", "or", "volume", "start_dt", ":", "Timestamp", "Beginning", "of", "the", "window", "range", ".", "end_dt", ":", "Timestamp", "End", "of", "the", "window", "range",...
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/data/continuous_future_reader.py#L204-L282
[ "def", "load_raw_arrays", "(", "self", ",", "columns", ",", "start_date", ",", "end_date", ",", "assets", ")", ":", "rolls_by_asset", "=", "{", "}", "tc", "=", "self", ".", "trading_calendar", "start_session", "=", "tc", ".", "minute_to_session_label", "(", ...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
Portfolio.current_portfolio_weights
Compute each asset's weight in the portfolio by calculating its held value divided by the total value of all positions. Each equity's value is its price times the number of shares held. Each futures contract's value is its unit price times number of shares held times the multiplier.
zipline/protocol.py
def current_portfolio_weights(self): """ Compute each asset's weight in the portfolio by calculating its held value divided by the total value of all positions. Each equity's value is its price times the number of shares held. Each futures contract's value is its unit price times number of shares held times the multiplier. """ position_values = pd.Series({ asset: ( position.last_sale_price * position.amount * asset.price_multiplier ) for asset, position in self.positions.items() }) return position_values / self.portfolio_value
def current_portfolio_weights(self): """ Compute each asset's weight in the portfolio by calculating its held value divided by the total value of all positions. Each equity's value is its price times the number of shares held. Each futures contract's value is its unit price times number of shares held times the multiplier. """ position_values = pd.Series({ asset: ( position.last_sale_price * position.amount * asset.price_multiplier ) for asset, position in self.positions.items() }) return position_values / self.portfolio_value
[ "Compute", "each", "asset", "s", "weight", "in", "the", "portfolio", "by", "calculating", "its", "held", "value", "divided", "by", "the", "total", "value", "of", "all", "positions", "." ]
quantopian/zipline
python
https://github.com/quantopian/zipline/blob/77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe/zipline/protocol.py#L216-L233
[ "def", "current_portfolio_weights", "(", "self", ")", ":", "position_values", "=", "pd", ".", "Series", "(", "{", "asset", ":", "(", "position", ".", "last_sale_price", "*", "position", ".", "amount", "*", "asset", ".", "price_multiplier", ")", "for", "asset...
77ad15e6dc4c1cbcdc133653bac8a63fc704f7fe
train
WechatSogouAPI.__hosting_wechat_img
将微信明细中图片托管到云端,同时将html页面中的对应图片替换 Parameters ---------- content_info : dict 微信文章明细字典 { 'content_img_list': [], # 从微信文章解析出的原始图片列表 'content_html': '', # 从微信文章解析出文章的内容 } hosting_callback : callable 托管回调函数,传入单个图片链接,返回托管后的图片链接 Returns ------- dict { 'content_img_list': '', # 托管后的图片列表 'content_html': '', # 图片链接为托管后的图片链接内容 }
wechatsogou/api.py
def __hosting_wechat_img(self, content_info, hosting_callback): """将微信明细中图片托管到云端,同时将html页面中的对应图片替换 Parameters ---------- content_info : dict 微信文章明细字典 { 'content_img_list': [], # 从微信文章解析出的原始图片列表 'content_html': '', # 从微信文章解析出文章的内容 } hosting_callback : callable 托管回调函数,传入单个图片链接,返回托管后的图片链接 Returns ------- dict { 'content_img_list': '', # 托管后的图片列表 'content_html': '', # 图片链接为托管后的图片链接内容 } """ assert callable(hosting_callback) content_img_list = content_info.pop("content_img_list") content_html = content_info.pop("content_html") for idx, img_url in enumerate(content_img_list): hosting_img_url = hosting_callback(img_url) if not hosting_img_url: # todo 定义标准异常 raise Exception() content_img_list[idx] = hosting_img_url content_html = content_html.replace(img_url, hosting_img_url) return dict(content_img_list=content_img_list, content_html=content_html)
def __hosting_wechat_img(self, content_info, hosting_callback): """将微信明细中图片托管到云端,同时将html页面中的对应图片替换 Parameters ---------- content_info : dict 微信文章明细字典 { 'content_img_list': [], # 从微信文章解析出的原始图片列表 'content_html': '', # 从微信文章解析出文章的内容 } hosting_callback : callable 托管回调函数,传入单个图片链接,返回托管后的图片链接 Returns ------- dict { 'content_img_list': '', # 托管后的图片列表 'content_html': '', # 图片链接为托管后的图片链接内容 } """ assert callable(hosting_callback) content_img_list = content_info.pop("content_img_list") content_html = content_info.pop("content_html") for idx, img_url in enumerate(content_img_list): hosting_img_url = hosting_callback(img_url) if not hosting_img_url: # todo 定义标准异常 raise Exception() content_img_list[idx] = hosting_img_url content_html = content_html.replace(img_url, hosting_img_url) return dict(content_img_list=content_img_list, content_html=content_html)
[ "将微信明细中图片托管到云端,同时将html页面中的对应图片替换" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/api.py#L138-L171
[ "def", "__hosting_wechat_img", "(", "self", ",", "content_info", ",", "hosting_callback", ")", ":", "assert", "callable", "(", "hosting_callback", ")", "content_img_list", "=", "content_info", ".", "pop", "(", "\"content_img_list\"", ")", "content_html", "=", "conte...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouAPI.get_gzh_info
获取公众号微信号 wechatid 的信息 因为wechatid唯一确定,所以第一个就是要搜索的公众号 Parameters ---------- wecgat_id_or_name : str or unicode wechat_id or wechat_name unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict or None { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 }
wechatsogou/api.py
def get_gzh_info(self, wecgat_id_or_name, unlock_callback=None, identify_image_callback=None, decode_url=True): """获取公众号微信号 wechatid 的信息 因为wechatid唯一确定,所以第一个就是要搜索的公众号 Parameters ---------- wecgat_id_or_name : str or unicode wechat_id or wechat_name unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict or None { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } """ info = self.search_gzh(wecgat_id_or_name, 1, unlock_callback, identify_image_callback, decode_url) try: return next(info) except StopIteration: return None
def get_gzh_info(self, wecgat_id_or_name, unlock_callback=None, identify_image_callback=None, decode_url=True): """获取公众号微信号 wechatid 的信息 因为wechatid唯一确定,所以第一个就是要搜索的公众号 Parameters ---------- wecgat_id_or_name : str or unicode wechat_id or wechat_name unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict or None { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } """ info = self.search_gzh(wecgat_id_or_name, 1, unlock_callback, identify_image_callback, decode_url) try: return next(info) except StopIteration: return None
[ "获取公众号微信号", "wechatid", "的信息" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/api.py#L208-L241
[ "def", "get_gzh_info", "(", "self", ",", "wecgat_id_or_name", ",", "unlock_callback", "=", "None", ",", "identify_image_callback", "=", "None", ",", "decode_url", "=", "True", ")", ":", "info", "=", "self", ".", "search_gzh", "(", "wecgat_id_or_name", ",", "1"...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouAPI.search_gzh
搜索 公众号 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } Raises ------ WechatSogouRequestsException requests error
wechatsogou/api.py
def search_gzh(self, keyword, page=1, unlock_callback=None, identify_image_callback=None, decode_url=True): """搜索 公众号 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } Raises ------ WechatSogouRequestsException requests error """ url = WechatSogouRequest.gen_search_gzh_url(keyword, page) session = requests.session() resp = self.__get_by_unlock(url, unlock_platform=self.__unlock_sogou, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) gzh_list = WechatSogouStructuring.get_gzh_by_search(resp.text) for i in gzh_list: if decode_url: i['profile_url'] = self.__format_url(i['profile_url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) yield i
def search_gzh(self, keyword, page=1, unlock_callback=None, identify_image_callback=None, decode_url=True): """搜索 公众号 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } Raises ------ WechatSogouRequestsException requests error """ url = WechatSogouRequest.gen_search_gzh_url(keyword, page) session = requests.session() resp = self.__get_by_unlock(url, unlock_platform=self.__unlock_sogou, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) gzh_list = WechatSogouStructuring.get_gzh_by_search(resp.text) for i in gzh_list: if decode_url: i['profile_url'] = self.__format_url(i['profile_url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) yield i
[ "搜索", "公众号" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/api.py#L243-L296
[ "def", "search_gzh", "(", "self", ",", "keyword", ",", "page", "=", "1", ",", "unlock_callback", "=", "None", ",", "identify_image_callback", "=", "None", ",", "decode_url", "=", "True", ")", ":", "url", "=", "WechatSogouRequest", ".", "gen_search_gzh_url", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouAPI.search_article
搜索 文章 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 the default is anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 ft, et : datetime.date or None 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } } Raises ------ WechatSogouRequestsException requests error
wechatsogou/api.py
def search_article(self, keyword, page=1, timesn=WechatSogouConst.search_article_time.anytime, article_type=WechatSogouConst.search_article_type.all, ft=None, et=None, unlock_callback=None, identify_image_callback=None, decode_url=True): """搜索 文章 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 the default is anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 ft, et : datetime.date or None 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } } Raises ------ WechatSogouRequestsException requests error """ url = WechatSogouRequest.gen_search_article_url(keyword, page, timesn, article_type, ft, et) session = requests.session() resp = self.__get_by_unlock(url, WechatSogouRequest.gen_search_article_url(keyword), unlock_platform=self.__unlock_sogou, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) article_list = WechatSogouStructuring.get_article_by_search(resp.text) for i in article_list: if decode_url: i['article']['url'] = self.__format_url(i['article']['url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) i['gzh']['profile_url'] = self.__format_url(i['gzh']['profile_url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) yield i
def search_article(self, keyword, page=1, timesn=WechatSogouConst.search_article_time.anytime, article_type=WechatSogouConst.search_article_type.all, ft=None, et=None, unlock_callback=None, identify_image_callback=None, decode_url=True): """搜索 文章 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 the default is anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 ft, et : datetime.date or None 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 unlock_callback : callable 处理出现验证码页面的函数,参见 unlock_callback_example identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example decode_url : bool 是否解析 url Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } } Raises ------ WechatSogouRequestsException requests error """ url = WechatSogouRequest.gen_search_article_url(keyword, page, timesn, article_type, ft, et) session = requests.session() resp = self.__get_by_unlock(url, WechatSogouRequest.gen_search_article_url(keyword), unlock_platform=self.__unlock_sogou, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) article_list = WechatSogouStructuring.get_article_by_search(resp.text) for i in article_list: if decode_url: i['article']['url'] = self.__format_url(i['article']['url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) i['gzh']['profile_url'] = self.__format_url(i['gzh']['profile_url'], url, resp.text, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback, session=session) yield i
[ "搜索", "文章" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/api.py#L298-L369
[ "def", "search_article", "(", "self", ",", "keyword", ",", "page", "=", "1", ",", "timesn", "=", "WechatSogouConst", ".", "search_article_time", ".", "anytime", ",", "article_type", "=", "WechatSogouConst", ".", "search_article_type", ".", "all", ",", "ft", "=...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouAPI.get_gzh_article_by_history
从 公众号的最近10条群发页面 提取公众号信息 和 文章列表信息 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 公众号的id 或者name url : str or unicode 群发页url,如果不提供url,就先去搜索一遍拿到url unlock_callback_sogou : callable 处理出现 搜索 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_sogou : callable 处理 搜索 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example unlock_callback_weixin : callable 处理出现 历史页 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_weixin : callable 处理 历史页 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'gzh': { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 }, 'article': [ { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 }, ... ] } Raises ------ WechatSogouRequestsException requests error
wechatsogou/api.py
def get_gzh_article_by_history(self, keyword=None, url=None, unlock_callback_sogou=None, identify_image_callback_sogou=None, unlock_callback_weixin=None, identify_image_callback_weixin=None): """从 公众号的最近10条群发页面 提取公众号信息 和 文章列表信息 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 公众号的id 或者name url : str or unicode 群发页url,如果不提供url,就先去搜索一遍拿到url unlock_callback_sogou : callable 处理出现 搜索 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_sogou : callable 处理 搜索 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example unlock_callback_weixin : callable 处理出现 历史页 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_weixin : callable 处理 历史页 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'gzh': { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 }, 'article': [ { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 }, ... ] } Raises ------ WechatSogouRequestsException requests error """ if url is None: gzh_list = self.get_gzh_info(keyword, unlock_callback_sogou, identify_image_callback_sogou) if gzh_list is None: return {} if 'profile_url' not in gzh_list: raise Exception() # todo use ws exception url = gzh_list['profile_url'] resp = self.__get_by_unlock(url, WechatSogouRequest.gen_search_article_url(keyword), unlock_platform=self.__unlock_wechat, unlock_callback=unlock_callback_weixin, identify_image_callback=identify_image_callback_weixin) return WechatSogouStructuring.get_gzh_info_and_article_by_history(resp.text)
def get_gzh_article_by_history(self, keyword=None, url=None, unlock_callback_sogou=None, identify_image_callback_sogou=None, unlock_callback_weixin=None, identify_image_callback_weixin=None): """从 公众号的最近10条群发页面 提取公众号信息 和 文章列表信息 对于出现验证码的情况,可以由使用者自己提供: 1、函数 unlock_callback ,这个函数 handle 出现验证码到解决的整个流程 2、也可以 只提供函数 identify_image_callback,这个函数输入验证码二进制数据,输出验证码文字,剩下的由 wechatsogou 包来解决 注意: 函数 unlock_callback 和 identify_image_callback 只需要提供一个,如果都提供了,那么 identify_image_callback 不起作用 Parameters ---------- keyword : str or unicode 公众号的id 或者name url : str or unicode 群发页url,如果不提供url,就先去搜索一遍拿到url unlock_callback_sogou : callable 处理出现 搜索 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_sogou : callable 处理 搜索 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example unlock_callback_weixin : callable 处理出现 历史页 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback_weixin : callable 处理 历史页 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'gzh': { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 }, 'article': [ { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 }, ... ] } Raises ------ WechatSogouRequestsException requests error """ if url is None: gzh_list = self.get_gzh_info(keyword, unlock_callback_sogou, identify_image_callback_sogou) if gzh_list is None: return {} if 'profile_url' not in gzh_list: raise Exception() # todo use ws exception url = gzh_list['profile_url'] resp = self.__get_by_unlock(url, WechatSogouRequest.gen_search_article_url(keyword), unlock_platform=self.__unlock_wechat, unlock_callback=unlock_callback_weixin, identify_image_callback=identify_image_callback_weixin) return WechatSogouStructuring.get_gzh_info_and_article_by_history(resp.text)
[ "从", "公众号的最近10条群发页面", "提取公众号信息", "和", "文章列表信息" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/api.py#L371-L448
[ "def", "get_gzh_article_by_history", "(", "self", ",", "keyword", "=", "None", ",", "url", "=", "None", ",", "unlock_callback_sogou", "=", "None", ",", "identify_image_callback_sogou", "=", "None", ",", "unlock_callback_weixin", "=", "None", ",", "identify_image_cal...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouAPI.get_gzh_article_by_hot
获取 首页热门文章 Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } }
wechatsogou/api.py
def get_gzh_article_by_hot(self, hot_index, page=1, unlock_callback=None, identify_image_callback=None): """获取 首页热门文章 Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } } """ assert hasattr(WechatSogouConst.hot_index, hot_index) assert isinstance(page, int) and page > 0 url = WechatSogouRequest.gen_hot_url(hot_index, page) resp = self.__get_by_unlock(url, unlock_platform=self.__unlock_sogou, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback) resp.encoding = 'utf-8' return WechatSogouStructuring.get_gzh_article_by_hot(resp.text)
def get_gzh_article_by_hot(self, hot_index, page=1, unlock_callback=None, identify_image_callback=None): """获取 首页热门文章 Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } } """ assert hasattr(WechatSogouConst.hot_index, hot_index) assert isinstance(page, int) and page > 0 url = WechatSogouRequest.gen_hot_url(hot_index, page) resp = self.__get_by_unlock(url, unlock_platform=self.__unlock_sogou, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback) resp.encoding = 'utf-8' return WechatSogouStructuring.get_gzh_article_by_hot(resp.text)
[ "获取", "首页热门文章" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/api.py#L450-L489
[ "def", "get_gzh_article_by_hot", "(", "self", ",", "hot_index", ",", "page", "=", "1", ",", "unlock_callback", "=", "None", ",", "identify_image_callback", "=", "None", ")", ":", "assert", "hasattr", "(", "WechatSogouConst", ".", "hot_index", ",", "hot_index", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouAPI.get_article_content
获取文章原文,避免临时链接失效 Parameters ---------- url : str or unicode 原文链接,临时链接 raw : bool True: 返回原始html False: 返回处理后的html del_qqmusic: bool True:微信原文中有插入的qq音乐,则删除 False:微信源文中有插入的qq音乐,则保留 del_mpvoice: bool True:微信原文中有插入的语音消息,则删除 False:微信源文中有插入的语音消息,则保留 unlock_callback : callable 处理 文章明细 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback : callable 处理 文章明细 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example hosting_callback: callable 将微信采集的文章托管到7牛或者阿里云回调函数,输入微信图片源地址,返回托管后地址 Returns ------- content_html 原文内容 content_img_list 文章中图片列表 Raises ------ WechatSogouRequestsException
wechatsogou/api.py
def get_article_content(self, url, del_qqmusic=True, del_mpvoice=True, unlock_callback=None, identify_image_callback=None, hosting_callback=None, raw=False): """获取文章原文,避免临时链接失效 Parameters ---------- url : str or unicode 原文链接,临时链接 raw : bool True: 返回原始html False: 返回处理后的html del_qqmusic: bool True:微信原文中有插入的qq音乐,则删除 False:微信源文中有插入的qq音乐,则保留 del_mpvoice: bool True:微信原文中有插入的语音消息,则删除 False:微信源文中有插入的语音消息,则保留 unlock_callback : callable 处理 文章明细 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback : callable 处理 文章明细 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example hosting_callback: callable 将微信采集的文章托管到7牛或者阿里云回调函数,输入微信图片源地址,返回托管后地址 Returns ------- content_html 原文内容 content_img_list 文章中图片列表 Raises ------ WechatSogouRequestsException """ resp = self.__get_by_unlock(url, unlock_platform=self.__unlock_wechat, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback) resp.encoding = 'utf-8' if '链接已过期' in resp.text: raise WechatSogouException('get_article_content 链接 [{}] 已过期'.format(url)) if raw: return resp.text content_info = WechatSogouStructuring.get_article_detail(resp.text, del_qqmusic=del_qqmusic, del_voice=del_mpvoice) if hosting_callback: content_info = self.__hosting_wechat_img(content_info, hosting_callback) return content_info
def get_article_content(self, url, del_qqmusic=True, del_mpvoice=True, unlock_callback=None, identify_image_callback=None, hosting_callback=None, raw=False): """获取文章原文,避免临时链接失效 Parameters ---------- url : str or unicode 原文链接,临时链接 raw : bool True: 返回原始html False: 返回处理后的html del_qqmusic: bool True:微信原文中有插入的qq音乐,则删除 False:微信源文中有插入的qq音乐,则保留 del_mpvoice: bool True:微信原文中有插入的语音消息,则删除 False:微信源文中有插入的语音消息,则保留 unlock_callback : callable 处理 文章明细 的时候出现验证码的函数,参见 unlock_callback_example identify_image_callback : callable 处理 文章明细 的时候处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example hosting_callback: callable 将微信采集的文章托管到7牛或者阿里云回调函数,输入微信图片源地址,返回托管后地址 Returns ------- content_html 原文内容 content_img_list 文章中图片列表 Raises ------ WechatSogouRequestsException """ resp = self.__get_by_unlock(url, unlock_platform=self.__unlock_wechat, unlock_callback=unlock_callback, identify_image_callback=identify_image_callback) resp.encoding = 'utf-8' if '链接已过期' in resp.text: raise WechatSogouException('get_article_content 链接 [{}] 已过期'.format(url)) if raw: return resp.text content_info = WechatSogouStructuring.get_article_detail(resp.text, del_qqmusic=del_qqmusic, del_voice=del_mpvoice) if hosting_callback: content_info = self.__hosting_wechat_img(content_info, hosting_callback) return content_info
[ "获取文章原文,避免临时链接失效" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/api.py#L491-L541
[ "def", "get_article_content", "(", "self", ",", "url", ",", "del_qqmusic", "=", "True", ",", "del_mpvoice", "=", "True", ",", "unlock_callback", "=", "None", ",", "identify_image_callback", "=", "None", ",", "hosting_callback", "=", "None", ",", "raw", "=", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouAPI.get_sugg
获取微信搜狗搜索关键词联想 Parameters ---------- keyword : str or unicode 关键词 Returns ------- list[str] 联想关键词列表 Raises ------ WechatSogouRequestsException
wechatsogou/api.py
def get_sugg(self, keyword): """获取微信搜狗搜索关键词联想 Parameters ---------- keyword : str or unicode 关键词 Returns ------- list[str] 联想关键词列表 Raises ------ WechatSogouRequestsException """ url = 'http://w.sugg.sogou.com/sugg/ajaj_json.jsp?key={}&type=wxpub&pr=web'.format( quote(keyword.encode('utf-8'))) r = requests.get(url) if not r.ok: raise WechatSogouRequestsException('get_sugg', r) sugg = re.findall(u'\["' + keyword + '",(.*?),\["', r.text)[0] return json.loads(sugg)
def get_sugg(self, keyword): """获取微信搜狗搜索关键词联想 Parameters ---------- keyword : str or unicode 关键词 Returns ------- list[str] 联想关键词列表 Raises ------ WechatSogouRequestsException """ url = 'http://w.sugg.sogou.com/sugg/ajaj_json.jsp?key={}&type=wxpub&pr=web'.format( quote(keyword.encode('utf-8'))) r = requests.get(url) if not r.ok: raise WechatSogouRequestsException('get_sugg', r) sugg = re.findall(u'\["' + keyword + '",(.*?),\["', r.text)[0] return json.loads(sugg)
[ "获取微信搜狗搜索关键词联想" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/api.py#L543-L567
[ "def", "get_sugg", "(", "self", ",", "keyword", ")", ":", "url", "=", "'http://w.sugg.sogou.com/sugg/ajaj_json.jsp?key={}&type=wxpub&pr=web'", ".", "format", "(", "quote", "(", "keyword", ".", "encode", "(", "'utf-8'", ")", ")", ")", "r", "=", "requests", ".", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
unlock_sogou_callback_example
手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'code': '', 'msg': '', }
wechatsogou/identify_image.py
def unlock_sogou_callback_example(url, req, resp, img, identify_image_callback): """手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'code': '', 'msg': '', } """ # no use resp url_quote = url.split('weixin.sogou.com/')[-1] unlock_url = 'http://weixin.sogou.com/antispider/thank.php' data = { 'c': identify_image_callback(img), 'r': '%2F' + url_quote, 'v': 5 } headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer': 'http://weixin.sogou.com/antispider/?from=%2f' + url_quote } r_unlock = req.post(unlock_url, data, headers=headers) r_unlock.encoding = 'utf-8' if not r_unlock.ok: raise WechatSogouVcodeOcrException( 'unlock[{}] failed: {}'.format(unlock_url, r_unlock.text, r_unlock.status_code)) return r_unlock.json()
def unlock_sogou_callback_example(url, req, resp, img, identify_image_callback): """手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'code': '', 'msg': '', } """ # no use resp url_quote = url.split('weixin.sogou.com/')[-1] unlock_url = 'http://weixin.sogou.com/antispider/thank.php' data = { 'c': identify_image_callback(img), 'r': '%2F' + url_quote, 'v': 5 } headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer': 'http://weixin.sogou.com/antispider/?from=%2f' + url_quote } r_unlock = req.post(unlock_url, data, headers=headers) r_unlock.encoding = 'utf-8' if not r_unlock.ok: raise WechatSogouVcodeOcrException( 'unlock[{}] failed: {}'.format(unlock_url, r_unlock.text, r_unlock.status_code)) return r_unlock.json()
[ "手动打码解锁" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/identify_image.py#L34-L76
[ "def", "unlock_sogou_callback_example", "(", "url", ",", "req", ",", "resp", ",", "img", ",", "identify_image_callback", ")", ":", "# no use resp", "url_quote", "=", "url", ".", "split", "(", "'weixin.sogou.com/'", ")", "[", "-", "1", "]", "unlock_url", "=", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
unlock_weixin_callback_example
手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'ret': '', 'errmsg': '', 'cookie_count': '', }
wechatsogou/identify_image.py
def unlock_weixin_callback_example(url, req, resp, img, identify_image_callback): """手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'ret': '', 'errmsg': '', 'cookie_count': '', } """ # no use resp unlock_url = 'https://mp.weixin.qq.com/mp/verifycode' data = { 'cert': time.time() * 1000, 'input': identify_image_callback(img) } headers = { 'Host': 'mp.weixin.qq.com', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer': url } r_unlock = req.post(unlock_url, data, headers=headers) if not r_unlock.ok: raise WechatSogouVcodeOcrException( 'unlock[{}] failed: {}[{}]'.format(unlock_url, r_unlock.text, r_unlock.status_code)) return r_unlock.json()
def unlock_weixin_callback_example(url, req, resp, img, identify_image_callback): """手动打码解锁 Parameters ---------- url : str or unicode 验证码页面 之前的 url req : requests.sessions.Session requests.Session() 供调用解锁 resp : requests.models.Response requests 访问页面返回的,已经跳转了 img : bytes 验证码图片二进制数据 identify_image_callback : callable 处理验证码函数,输入验证码二进制数据,输出文字,参见 identify_image_callback_example Returns ------- dict { 'ret': '', 'errmsg': '', 'cookie_count': '', } """ # no use resp unlock_url = 'https://mp.weixin.qq.com/mp/verifycode' data = { 'cert': time.time() * 1000, 'input': identify_image_callback(img) } headers = { 'Host': 'mp.weixin.qq.com', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Referer': url } r_unlock = req.post(unlock_url, data, headers=headers) if not r_unlock.ok: raise WechatSogouVcodeOcrException( 'unlock[{}] failed: {}[{}]'.format(unlock_url, r_unlock.text, r_unlock.status_code)) return r_unlock.json()
[ "手动打码解锁" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/identify_image.py#L79-L121
[ "def", "unlock_weixin_callback_example", "(", "url", ",", "req", ",", "resp", ",", "img", ",", "identify_image_callback", ")", ":", "# no use resp", "unlock_url", "=", "'https://mp.weixin.qq.com/mp/verifycode'", "data", "=", "{", "'cert'", ":", "time", ".", "time", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouRequest.gen_search_article_url
拼接搜索 文章 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 默认是 anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 默认是 all ft, et : datetime.date 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 Returns ------- str search_article_url
wechatsogou/request.py
def gen_search_article_url(keyword, page=1, timesn=WechatSogouConst.search_article_time.anytime, article_type=WechatSogouConst.search_article_type.all, ft=None, et=None): """拼接搜索 文章 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 默认是 anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 默认是 all ft, et : datetime.date 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 Returns ------- str search_article_url """ assert isinstance(page, int) and page > 0 assert timesn in [WechatSogouConst.search_article_time.anytime, WechatSogouConst.search_article_time.day, WechatSogouConst.search_article_time.week, WechatSogouConst.search_article_time.month, WechatSogouConst.search_article_time.year, WechatSogouConst.search_article_time.specific] if timesn == WechatSogouConst.search_article_time.specific: assert isinstance(ft, datetime.date) assert isinstance(et, datetime.date) assert ft <= et else: ft = '' et = '' interation_image = 458754 interation_video = 458756 if article_type == WechatSogouConst.search_article_type.rich: interation = '{},{}'.format(interation_image, interation_video) elif article_type == WechatSogouConst.search_article_type.image: interation = interation_image elif article_type == WechatSogouConst.search_article_type.video: interation = interation_video else: interation = '' qs_dict = OrderedDict() qs_dict['type'] = _search_type_article qs_dict['page'] = page qs_dict['ie'] = 'utf8' qs_dict['query'] = keyword qs_dict['interation'] = interation if timesn != 0: qs_dict['tsn'] = timesn qs_dict['ft'] = str(ft) qs_dict['et'] = str(et) # TODO 账号内搜索 # '账号内 http://weixin.sogou.com/weixin?type=2&ie=utf8&query=%E9%AB%98%E8%80%83&tsn=3&ft=&et=&interation=458754 # &wxid=oIWsFt1tmWoG6vO6BcsS7St61bRE&usip=nanhangqinggong' # qs['wxid'] = wxid # qs['usip'] = usip return 'http://weixin.sogou.com/weixin?{}'.format(urlencode(qs_dict))
def gen_search_article_url(keyword, page=1, timesn=WechatSogouConst.search_article_time.anytime, article_type=WechatSogouConst.search_article_type.all, ft=None, et=None): """拼接搜索 文章 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 timesn : WechatSogouConst.search_article_time 时间 anytime 没有限制 / day 一天 / week 一周 / month 一月 / year 一年 / specific 自定 默认是 anytime article_type : WechatSogouConst.search_article_type 含有内容的类型 image 有图 / video 有视频 / rich 有图和视频 / all 啥都有 默认是 all ft, et : datetime.date 当 tsn 是 specific 时,ft 代表开始时间,如: 2017-07-01 当 tsn 是 specific 时,et 代表结束时间,如: 2017-07-15 Returns ------- str search_article_url """ assert isinstance(page, int) and page > 0 assert timesn in [WechatSogouConst.search_article_time.anytime, WechatSogouConst.search_article_time.day, WechatSogouConst.search_article_time.week, WechatSogouConst.search_article_time.month, WechatSogouConst.search_article_time.year, WechatSogouConst.search_article_time.specific] if timesn == WechatSogouConst.search_article_time.specific: assert isinstance(ft, datetime.date) assert isinstance(et, datetime.date) assert ft <= et else: ft = '' et = '' interation_image = 458754 interation_video = 458756 if article_type == WechatSogouConst.search_article_type.rich: interation = '{},{}'.format(interation_image, interation_video) elif article_type == WechatSogouConst.search_article_type.image: interation = interation_image elif article_type == WechatSogouConst.search_article_type.video: interation = interation_video else: interation = '' qs_dict = OrderedDict() qs_dict['type'] = _search_type_article qs_dict['page'] = page qs_dict['ie'] = 'utf8' qs_dict['query'] = keyword qs_dict['interation'] = interation if timesn != 0: qs_dict['tsn'] = timesn qs_dict['ft'] = str(ft) qs_dict['et'] = str(et) # TODO 账号内搜索 # '账号内 http://weixin.sogou.com/weixin?type=2&ie=utf8&query=%E9%AB%98%E8%80%83&tsn=3&ft=&et=&interation=458754 # &wxid=oIWsFt1tmWoG6vO6BcsS7St61bRE&usip=nanhangqinggong' # qs['wxid'] = wxid # qs['usip'] = usip return 'http://weixin.sogou.com/weixin?{}'.format(urlencode(qs_dict))
[ "拼接搜索", "文章", "URL" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/request.py#L17-L86
[ "def", "gen_search_article_url", "(", "keyword", ",", "page", "=", "1", ",", "timesn", "=", "WechatSogouConst", ".", "search_article_time", ".", "anytime", ",", "article_type", "=", "WechatSogouConst", ".", "search_article_type", ".", "all", ",", "ft", "=", "Non...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouRequest.gen_search_gzh_url
拼接搜索 公众号 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 Returns ------- str search_gzh_url
wechatsogou/request.py
def gen_search_gzh_url(keyword, page=1): """拼接搜索 公众号 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 Returns ------- str search_gzh_url """ assert isinstance(page, int) and page > 0 qs_dict = OrderedDict() qs_dict['type'] = _search_type_gzh qs_dict['page'] = page qs_dict['ie'] = 'utf8' qs_dict['query'] = keyword return 'http://weixin.sogou.com/weixin?{}'.format(urlencode(qs_dict))
def gen_search_gzh_url(keyword, page=1): """拼接搜索 公众号 URL Parameters ---------- keyword : str or unicode 搜索文字 page : int, optional 页数 the default is 1 Returns ------- str search_gzh_url """ assert isinstance(page, int) and page > 0 qs_dict = OrderedDict() qs_dict['type'] = _search_type_gzh qs_dict['page'] = page qs_dict['ie'] = 'utf8' qs_dict['query'] = keyword return 'http://weixin.sogou.com/weixin?{}'.format(urlencode(qs_dict))
[ "拼接搜索", "公众号", "URL" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/request.py#L89-L112
[ "def", "gen_search_gzh_url", "(", "keyword", ",", "page", "=", "1", ")", ":", "assert", "isinstance", "(", "page", ",", "int", ")", "and", "page", ">", "0", "qs_dict", "=", "OrderedDict", "(", ")", "qs_dict", "[", "'type'", "]", "=", "_search_type_gzh", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouRequest.gen_hot_url
拼接 首页热门文章 URL Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- str 热门文章分类的url
wechatsogou/request.py
def gen_hot_url(hot_index, page=1): """拼接 首页热门文章 URL Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- str 热门文章分类的url """ assert hasattr(WechatSogouConst.hot_index, hot_index) assert isinstance(page, int) and page > 0 index_urls = { WechatSogouConst.hot_index.hot: 0, # 热门 WechatSogouConst.hot_index.gaoxiao: 1, # 搞笑 WechatSogouConst.hot_index.health: 2, # 养生 WechatSogouConst.hot_index.sifanghua: 3, # 私房话 WechatSogouConst.hot_index.gossip: 4, # 八卦 WechatSogouConst.hot_index.technology: 5, # 科技 WechatSogouConst.hot_index.finance: 6, # 财经 WechatSogouConst.hot_index.car: 7, # 汽车 WechatSogouConst.hot_index.life: 8, # 生活 WechatSogouConst.hot_index.fashion: 9, # 时尚 WechatSogouConst.hot_index.mummy: 10, # 辣妈 / 育儿 WechatSogouConst.hot_index.travel: 11, # 旅行 WechatSogouConst.hot_index.job: 12, # 职场 WechatSogouConst.hot_index.food: 13, # 美食 WechatSogouConst.hot_index.history: 14, # 历史 WechatSogouConst.hot_index.study: 15, # 学霸 / 教育 WechatSogouConst.hot_index.constellation: 16, # 星座 WechatSogouConst.hot_index.sport: 17, # 体育 WechatSogouConst.hot_index.military: 18, # 军事 WechatSogouConst.hot_index.game: 19, # 游戏 WechatSogouConst.hot_index.pet: 20, # 萌宠 } return 'http://weixin.sogou.com/wapindex/wap/0612/wap_{}/{}.html'.format(index_urls[hot_index], page - 1)
def gen_hot_url(hot_index, page=1): """拼接 首页热门文章 URL Parameters ---------- hot_index : WechatSogouConst.hot_index 首页热门文章的分类(常量):WechatSogouConst.hot_index.xxx page : int 页数 Returns ------- str 热门文章分类的url """ assert hasattr(WechatSogouConst.hot_index, hot_index) assert isinstance(page, int) and page > 0 index_urls = { WechatSogouConst.hot_index.hot: 0, # 热门 WechatSogouConst.hot_index.gaoxiao: 1, # 搞笑 WechatSogouConst.hot_index.health: 2, # 养生 WechatSogouConst.hot_index.sifanghua: 3, # 私房话 WechatSogouConst.hot_index.gossip: 4, # 八卦 WechatSogouConst.hot_index.technology: 5, # 科技 WechatSogouConst.hot_index.finance: 6, # 财经 WechatSogouConst.hot_index.car: 7, # 汽车 WechatSogouConst.hot_index.life: 8, # 生活 WechatSogouConst.hot_index.fashion: 9, # 时尚 WechatSogouConst.hot_index.mummy: 10, # 辣妈 / 育儿 WechatSogouConst.hot_index.travel: 11, # 旅行 WechatSogouConst.hot_index.job: 12, # 职场 WechatSogouConst.hot_index.food: 13, # 美食 WechatSogouConst.hot_index.history: 14, # 历史 WechatSogouConst.hot_index.study: 15, # 学霸 / 教育 WechatSogouConst.hot_index.constellation: 16, # 星座 WechatSogouConst.hot_index.sport: 17, # 体育 WechatSogouConst.hot_index.military: 18, # 军事 WechatSogouConst.hot_index.game: 19, # 游戏 WechatSogouConst.hot_index.pet: 20, # 萌宠 } return 'http://weixin.sogou.com/wapindex/wap/0612/wap_{}/{}.html'.format(index_urls[hot_index], page - 1)
[ "拼接", "首页热门文章", "URL" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/request.py#L115-L158
[ "def", "gen_hot_url", "(", "hot_index", ",", "page", "=", "1", ")", ":", "assert", "hasattr", "(", "WechatSogouConst", ".", "hot_index", ",", "hot_index", ")", "assert", "isinstance", "(", "page", ",", "int", ")", "and", "page", ">", "0", "index_urls", "...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
get_first_of_element
抽取lxml.etree库中elem对象中文字 Args: element: lxml.etree.Element sub: str Returns: elem中文字
wechatsogou/tools.py
def get_first_of_element(element, sub, contype=None): """抽取lxml.etree库中elem对象中文字 Args: element: lxml.etree.Element sub: str Returns: elem中文字 """ content = element.xpath(sub) return list_or_empty(content, contype)
def get_first_of_element(element, sub, contype=None): """抽取lxml.etree库中elem对象中文字 Args: element: lxml.etree.Element sub: str Returns: elem中文字 """ content = element.xpath(sub) return list_or_empty(content, contype)
[ "抽取lxml", ".", "etree库中elem对象中文字" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/tools.py#L46-L57
[ "def", "get_first_of_element", "(", "element", ",", "sub", ",", "contype", "=", "None", ")", ":", "content", "=", "element", ".", "xpath", "(", "sub", ")", "return", "list_or_empty", "(", "content", ",", "contype", ")" ]
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
get_encoding_from_reponse
获取requests库get或post返回的对象编码 Args: r: requests库get或post返回的对象 Returns: 对象编码
wechatsogou/tools.py
def get_encoding_from_reponse(r): """获取requests库get或post返回的对象编码 Args: r: requests库get或post返回的对象 Returns: 对象编码 """ encoding = requests.utils.get_encodings_from_content(r.text) return encoding[0] if encoding else requests.utils.get_encoding_from_headers(r.headers)
def get_encoding_from_reponse(r): """获取requests库get或post返回的对象编码 Args: r: requests库get或post返回的对象 Returns: 对象编码 """ encoding = requests.utils.get_encodings_from_content(r.text) return encoding[0] if encoding else requests.utils.get_encoding_from_headers(r.headers)
[ "获取requests库get或post返回的对象编码" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/tools.py#L60-L70
[ "def", "get_encoding_from_reponse", "(", "r", ")", ":", "encoding", "=", "requests", ".", "utils", ".", "get_encodings_from_content", "(", "r", ".", "text", ")", "return", "encoding", "[", "0", "]", "if", "encoding", "else", "requests", ".", "utils", ".", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
_replace_str_html
替换html‘&quot;’等转义内容为正常内容 Args: s: 文字内容 Returns: s: 处理反转义后的文字
wechatsogou/tools.py
def _replace_str_html(s): """替换html‘&quot;’等转义内容为正常内容 Args: s: 文字内容 Returns: s: 处理反转义后的文字 """ html_str_list = [ ('&#39;', '\''), ('&quot;', '"'), ('&amp;', '&'), ('&yen;', '¥'), ('amp;', ''), ('&lt;', '<'), ('&gt;', '>'), ('&nbsp;', ' '), ('\\', '') ] for i in html_str_list: s = s.replace(i[0], i[1]) return s
def _replace_str_html(s): """替换html‘&quot;’等转义内容为正常内容 Args: s: 文字内容 Returns: s: 处理反转义后的文字 """ html_str_list = [ ('&#39;', '\''), ('&quot;', '"'), ('&amp;', '&'), ('&yen;', '¥'), ('amp;', ''), ('&lt;', '<'), ('&gt;', '>'), ('&nbsp;', ' '), ('\\', '') ] for i in html_str_list: s = s.replace(i[0], i[1]) return s
[ "替换html‘&quot", ";", "’等转义内容为正常内容" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/tools.py#L73-L95
[ "def", "_replace_str_html", "(", "s", ")", ":", "html_str_list", "=", "[", "(", "'&#39;'", ",", "'\\''", ")", ",", "(", "'&quot;'", ",", "'\"'", ")", ",", "(", "'&amp;'", ",", "'&'", ")", ",", "(", "'&yen;'", ",", "'¥')", ",", "", "(", "'amp;'", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouStructuring.get_gzh_by_search
从搜索公众号获得的文本 提取公众号信息 Parameters ---------- text : str or unicode 搜索公众号获得的文本 Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'view_perm': '', # 最近一月阅读量 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 }
wechatsogou/structuring.py
def get_gzh_by_search(text): """从搜索公众号获得的文本 提取公众号信息 Parameters ---------- text : str or unicode 搜索公众号获得的文本 Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'view_perm': '', # 最近一月阅读量 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } """ post_view_perms = WechatSogouStructuring.__get_post_view_perm(text) page = etree.HTML(text) lis = page.xpath('//ul[@class="news-list2"]/li') relist = [] for li in lis: url = get_first_of_element(li, 'div/div[1]/a/@href') headimage = format_image_url(get_first_of_element(li, 'div/div[1]/a/img/@src')) wechat_name = get_elem_text(get_first_of_element(li, 'div/div[2]/p[1]')) info = get_elem_text(get_first_of_element(li, 'div/div[2]/p[2]')) qrcode = get_first_of_element(li, 'div/div[3]/span/img[1]/@src') introduction = get_elem_text(get_first_of_element(li, 'dl[1]/dd')) authentication = get_first_of_element(li, 'dl[2]/dd/text()') relist.append({ 'open_id': headimage.split('/')[-1], 'profile_url': url, 'headimage': headimage, 'wechat_name': wechat_name.replace('red_beg', '').replace('red_end', ''), 'wechat_id': info.replace('微信号:', ''), 'qrcode': qrcode, 'introduction': introduction.replace('red_beg', '').replace('red_end', ''), 'authentication': authentication, 'post_perm': -1, 'view_perm': -1, }) if post_view_perms: for i in relist: if i['open_id'] in post_view_perms: post_view_perm = post_view_perms[i['open_id']].split(',') if len(post_view_perm) == 2: i['post_perm'] = int(post_view_perm[0]) i['view_perm'] = int(post_view_perm[1]) return relist
def get_gzh_by_search(text): """从搜索公众号获得的文本 提取公众号信息 Parameters ---------- text : str or unicode 搜索公众号获得的文本 Returns ------- list[dict] { 'open_id': '', # 微信号唯一ID 'profile_url': '', # 最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'post_perm': '', # 最近一月群发数 'view_perm': '', # 最近一月阅读量 'qrcode': '', # 二维码 'introduction': '', # 介绍 'authentication': '' # 认证 } """ post_view_perms = WechatSogouStructuring.__get_post_view_perm(text) page = etree.HTML(text) lis = page.xpath('//ul[@class="news-list2"]/li') relist = [] for li in lis: url = get_first_of_element(li, 'div/div[1]/a/@href') headimage = format_image_url(get_first_of_element(li, 'div/div[1]/a/img/@src')) wechat_name = get_elem_text(get_first_of_element(li, 'div/div[2]/p[1]')) info = get_elem_text(get_first_of_element(li, 'div/div[2]/p[2]')) qrcode = get_first_of_element(li, 'div/div[3]/span/img[1]/@src') introduction = get_elem_text(get_first_of_element(li, 'dl[1]/dd')) authentication = get_first_of_element(li, 'dl[2]/dd/text()') relist.append({ 'open_id': headimage.split('/')[-1], 'profile_url': url, 'headimage': headimage, 'wechat_name': wechat_name.replace('red_beg', '').replace('red_end', ''), 'wechat_id': info.replace('微信号:', ''), 'qrcode': qrcode, 'introduction': introduction.replace('red_beg', '').replace('red_end', ''), 'authentication': authentication, 'post_perm': -1, 'view_perm': -1, }) if post_view_perms: for i in relist: if i['open_id'] in post_view_perms: post_view_perm = post_view_perms[i['open_id']].split(',') if len(post_view_perm) == 2: i['post_perm'] = int(post_view_perm[0]) i['view_perm'] = int(post_view_perm[1]) return relist
[ "从搜索公众号获得的文本", "提取公众号信息" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/structuring.py#L46-L104
[ "def", "get_gzh_by_search", "(", "text", ")", ":", "post_view_perms", "=", "WechatSogouStructuring", ".", "__get_post_view_perm", "(", "text", ")", "page", "=", "etree", ".", "HTML", "(", "text", ")", "lis", "=", "page", ".", "xpath", "(", "'//ul[@class=\"news...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouStructuring.get_article_by_search
从搜索文章获得的文本 提取章列表信息 Parameters ---------- text : str or unicode 搜索文章获得的文本 Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } }
wechatsogou/structuring.py
def get_article_by_search(text): """从搜索文章获得的文本 提取章列表信息 Parameters ---------- text : str or unicode 搜索文章获得的文本 Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } } """ page = etree.HTML(text) lis = page.xpath('//ul[@class="news-list"]/li') articles = [] for li in lis: url = get_first_of_element(li, 'div[1]/a/@href') if url: title = get_first_of_element(li, 'div[2]/h3/a') imgs = li.xpath('div[1]/a/img/@src') abstract = get_first_of_element(li, 'div[2]/p') time = get_first_of_element(li, 'div[2]/div/span/script/text()') gzh_info = li.xpath('div[2]/div/a')[0] else: url = get_first_of_element(li, 'div/h3/a/@href') title = get_first_of_element(li, 'div/h3/a') imgs = [] spans = li.xpath('div/div[1]/a') for span in spans: img = span.xpath('span/img/@src') if img: imgs.append(img) abstract = get_first_of_element(li, 'div/p') time = get_first_of_element(li, 'div/div[2]/span/script/text()') gzh_info = li.xpath('div/div[2]/a')[0] if title is not None: title = get_elem_text(title).replace("red_beg", "").replace("red_end", "") if abstract is not None: abstract = get_elem_text(abstract).replace("red_beg", "").replace("red_end", "") time = re.findall('timeConvert\(\'(.*?)\'\)', time) time = list_or_empty(time, int) profile_url = get_first_of_element(gzh_info, '@href') headimage = get_first_of_element(gzh_info, '@data-headimage') wechat_name = get_first_of_element(gzh_info, 'text()') gzh_isv = get_first_of_element(gzh_info, '@data-isv', int) articles.append({ 'article': { 'title': title, 'url': url, 'imgs': format_image_url(imgs), 'abstract': abstract, 'time': time }, 'gzh': { 'profile_url': profile_url, 'headimage': headimage, 'wechat_name': wechat_name, 'isv': gzh_isv, } }) return articles
def get_article_by_search(text): """从搜索文章获得的文本 提取章列表信息 Parameters ---------- text : str or unicode 搜索文章获得的文本 Returns ------- list[dict] { 'article': { 'title': '', # 文章标题 'url': '', # 文章链接 'imgs': '', # 文章图片list 'abstract': '', # 文章摘要 'time': '' # 文章推送时间 }, 'gzh': { 'profile_url': '', # 公众号最近10条群发页链接 'headimage': '', # 头像 'wechat_name': '', # 名称 'isv': '', # 是否加v } } """ page = etree.HTML(text) lis = page.xpath('//ul[@class="news-list"]/li') articles = [] for li in lis: url = get_first_of_element(li, 'div[1]/a/@href') if url: title = get_first_of_element(li, 'div[2]/h3/a') imgs = li.xpath('div[1]/a/img/@src') abstract = get_first_of_element(li, 'div[2]/p') time = get_first_of_element(li, 'div[2]/div/span/script/text()') gzh_info = li.xpath('div[2]/div/a')[0] else: url = get_first_of_element(li, 'div/h3/a/@href') title = get_first_of_element(li, 'div/h3/a') imgs = [] spans = li.xpath('div/div[1]/a') for span in spans: img = span.xpath('span/img/@src') if img: imgs.append(img) abstract = get_first_of_element(li, 'div/p') time = get_first_of_element(li, 'div/div[2]/span/script/text()') gzh_info = li.xpath('div/div[2]/a')[0] if title is not None: title = get_elem_text(title).replace("red_beg", "").replace("red_end", "") if abstract is not None: abstract = get_elem_text(abstract).replace("red_beg", "").replace("red_end", "") time = re.findall('timeConvert\(\'(.*?)\'\)', time) time = list_or_empty(time, int) profile_url = get_first_of_element(gzh_info, '@href') headimage = get_first_of_element(gzh_info, '@data-headimage') wechat_name = get_first_of_element(gzh_info, 'text()') gzh_isv = get_first_of_element(gzh_info, '@data-isv', int) articles.append({ 'article': { 'title': title, 'url': url, 'imgs': format_image_url(imgs), 'abstract': abstract, 'time': time }, 'gzh': { 'profile_url': profile_url, 'headimage': headimage, 'wechat_name': wechat_name, 'isv': gzh_isv, } }) return articles
[ "从搜索文章获得的文本", "提取章列表信息" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/structuring.py#L136-L215
[ "def", "get_article_by_search", "(", "text", ")", ":", "page", "=", "etree", ".", "HTML", "(", "text", ")", "lis", "=", "page", ".", "xpath", "(", "'//ul[@class=\"news-list\"]/li'", ")", "articles", "=", "[", "]", "for", "li", "in", "lis", ":", "url", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouStructuring.get_gzh_info_by_history
从 历史消息页的文本 提取公众号信息 Parameters ---------- text : str or unicode 历史消息页的文本 Returns ------- dict { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 }
wechatsogou/structuring.py
def get_gzh_info_by_history(text): """从 历史消息页的文本 提取公众号信息 Parameters ---------- text : str or unicode 历史消息页的文本 Returns ------- dict { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 } """ page = etree.HTML(text) profile_area = get_first_of_element(page, '//div[@class="profile_info_area"]') profile_img = get_first_of_element(profile_area, 'div[1]/span/img/@src') profile_name = get_first_of_element(profile_area, 'div[1]/div/strong/text()') profile_wechat_id = get_first_of_element(profile_area, 'div[1]/div/p/text()') profile_desc = get_first_of_element(profile_area, 'ul/li[1]/div/text()') profile_principal = get_first_of_element(profile_area, 'ul/li[2]/div/text()') return { 'wechat_name': profile_name.strip(), 'wechat_id': profile_wechat_id.replace('微信号: ', '').strip('\n'), 'introduction': profile_desc, 'authentication': profile_principal, 'headimage': profile_img }
def get_gzh_info_by_history(text): """从 历史消息页的文本 提取公众号信息 Parameters ---------- text : str or unicode 历史消息页的文本 Returns ------- dict { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 描述 'authentication': '', # 认证 'headimage': '' # 头像 } """ page = etree.HTML(text) profile_area = get_first_of_element(page, '//div[@class="profile_info_area"]') profile_img = get_first_of_element(profile_area, 'div[1]/span/img/@src') profile_name = get_first_of_element(profile_area, 'div[1]/div/strong/text()') profile_wechat_id = get_first_of_element(profile_area, 'div[1]/div/p/text()') profile_desc = get_first_of_element(profile_area, 'ul/li[1]/div/text()') profile_principal = get_first_of_element(profile_area, 'ul/li[2]/div/text()') return { 'wechat_name': profile_name.strip(), 'wechat_id': profile_wechat_id.replace('微信号: ', '').strip('\n'), 'introduction': profile_desc, 'authentication': profile_principal, 'headimage': profile_img }
[ "从", "历史消息页的文本", "提取公众号信息" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/structuring.py#L218-L253
[ "def", "get_gzh_info_by_history", "(", "text", ")", ":", "page", "=", "etree", ".", "HTML", "(", "text", ")", "profile_area", "=", "get_first_of_element", "(", "page", ",", "'//div[@class=\"profile_info_area\"]'", ")", "profile_img", "=", "get_first_of_element", "("...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouStructuring.get_article_by_history_json
从 历史消息页的文本 提取文章列表信息 Parameters ---------- text : str or unicode 历史消息页的文本 article_json : dict 历史消息页的文本 提取出来的文章json dict Returns ------- list[dict] { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 }
wechatsogou/structuring.py
def get_article_by_history_json(text, article_json=None): """从 历史消息页的文本 提取文章列表信息 Parameters ---------- text : str or unicode 历史消息页的文本 article_json : dict 历史消息页的文本 提取出来的文章json dict Returns ------- list[dict] { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 } """ if article_json is None: article_json = find_article_json_re.findall(text) if not article_json: return [] article_json = article_json[0] + '}}]}' article_json = json.loads(article_json) items = list() for listdic in article_json['list']: if str(listdic['comm_msg_info'].get('type', '')) != '49': continue comm_msg_info = listdic['comm_msg_info'] app_msg_ext_info = listdic['app_msg_ext_info'] send_id = comm_msg_info.get('id', '') msg_datetime = comm_msg_info.get('datetime', '') msg_type = str(comm_msg_info.get('type', '')) items.append({ 'send_id': send_id, 'datetime': msg_datetime, 'type': msg_type, 'main': 1, 'title': app_msg_ext_info.get('title', ''), 'abstract': app_msg_ext_info.get('digest', ''), 'fileid': app_msg_ext_info.get('fileid', ''), 'content_url': WechatSogouStructuring.__handle_content_url(app_msg_ext_info.get('content_url')), 'source_url': app_msg_ext_info.get('source_url', ''), 'cover': app_msg_ext_info.get('cover', ''), 'author': app_msg_ext_info.get('author', ''), 'copyright_stat': app_msg_ext_info.get('copyright_stat', '') }) if app_msg_ext_info.get('is_multi', 0) == 1: for multi_dict in app_msg_ext_info['multi_app_msg_item_list']: items.append({ 'send_id': send_id, 'datetime': msg_datetime, 'type': msg_type, 'main': 0, 'title': multi_dict.get('title', ''), 'abstract': multi_dict.get('digest', ''), 'fileid': multi_dict.get('fileid', ''), 'content_url': WechatSogouStructuring.__handle_content_url(multi_dict.get('content_url')), 'source_url': multi_dict.get('source_url', ''), 'cover': multi_dict.get('cover', ''), 'author': multi_dict.get('author', ''), 'copyright_stat': multi_dict.get('copyright_stat', '') }) return list(filter(lambda x: x['content_url'], items))
def get_article_by_history_json(text, article_json=None): """从 历史消息页的文本 提取文章列表信息 Parameters ---------- text : str or unicode 历史消息页的文本 article_json : dict 历史消息页的文本 提取出来的文章json dict Returns ------- list[dict] { 'send_id': '', # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': '', # 群发datatime 'type': '', # 消息类型,均是49,表示图文 'main': 0, # 是否是一次群发的第一次消息 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': '', # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': '', # 文章类型,例如:原创啊 } """ if article_json is None: article_json = find_article_json_re.findall(text) if not article_json: return [] article_json = article_json[0] + '}}]}' article_json = json.loads(article_json) items = list() for listdic in article_json['list']: if str(listdic['comm_msg_info'].get('type', '')) != '49': continue comm_msg_info = listdic['comm_msg_info'] app_msg_ext_info = listdic['app_msg_ext_info'] send_id = comm_msg_info.get('id', '') msg_datetime = comm_msg_info.get('datetime', '') msg_type = str(comm_msg_info.get('type', '')) items.append({ 'send_id': send_id, 'datetime': msg_datetime, 'type': msg_type, 'main': 1, 'title': app_msg_ext_info.get('title', ''), 'abstract': app_msg_ext_info.get('digest', ''), 'fileid': app_msg_ext_info.get('fileid', ''), 'content_url': WechatSogouStructuring.__handle_content_url(app_msg_ext_info.get('content_url')), 'source_url': app_msg_ext_info.get('source_url', ''), 'cover': app_msg_ext_info.get('cover', ''), 'author': app_msg_ext_info.get('author', ''), 'copyright_stat': app_msg_ext_info.get('copyright_stat', '') }) if app_msg_ext_info.get('is_multi', 0) == 1: for multi_dict in app_msg_ext_info['multi_app_msg_item_list']: items.append({ 'send_id': send_id, 'datetime': msg_datetime, 'type': msg_type, 'main': 0, 'title': multi_dict.get('title', ''), 'abstract': multi_dict.get('digest', ''), 'fileid': multi_dict.get('fileid', ''), 'content_url': WechatSogouStructuring.__handle_content_url(multi_dict.get('content_url')), 'source_url': multi_dict.get('source_url', ''), 'cover': multi_dict.get('cover', ''), 'author': multi_dict.get('author', ''), 'copyright_stat': multi_dict.get('copyright_stat', '') }) return list(filter(lambda x: x['content_url'], items))
[ "从", "历史消息页的文本", "提取文章列表信息" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/structuring.py#L256-L334
[ "def", "get_article_by_history_json", "(", "text", ",", "article_json", "=", "None", ")", ":", "if", "article_json", "is", "None", ":", "article_json", "=", "find_article_json_re", ".", "findall", "(", "text", ")", "if", "not", "article_json", ":", "return", "...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouStructuring.get_gzh_article_by_hot
从 首页热门搜索 提取公众号信息 和 文章列表信息 Parameters ---------- text : str or unicode 首页热门搜索 页 中 某一页 的文本 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } }
wechatsogou/structuring.py
def get_gzh_article_by_hot(text): """从 首页热门搜索 提取公众号信息 和 文章列表信息 Parameters ---------- text : str or unicode 首页热门搜索 页 中 某一页 的文本 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } } """ page = etree.HTML(text) lis = page.xpath('/html/body/li') gzh_article_list = [] for li in lis: url = get_first_of_element(li, 'div[1]/h4/a/@href') title = get_first_of_element(li, 'div[1]/h4/a/div/text()') abstract = get_first_of_element(li, 'div[1]/p[1]/text()') xpath_time = get_first_of_element(li, 'div[1]/p[2]') open_id = get_first_of_element(xpath_time, 'span/@data-openid') headimage = get_first_of_element(xpath_time, 'span/@data-headimage') gzh_name = get_first_of_element(xpath_time, 'span/text()') send_time = xpath_time.xpath('a/span/@data-lastmodified') main_img = get_first_of_element(li, 'div[2]/a/img/@src') try: send_time = int(send_time[0]) except ValueError: send_time = send_time[0] gzh_article_list.append({ 'gzh': { 'headimage': headimage, 'wechat_name': gzh_name, }, 'article': { 'url': url, 'title': title, 'abstract': abstract, 'time': send_time, 'open_id': open_id, 'main_img': main_img } }) return gzh_article_list
def get_gzh_article_by_hot(text): """从 首页热门搜索 提取公众号信息 和 文章列表信息 Parameters ---------- text : str or unicode 首页热门搜索 页 中 某一页 的文本 Returns ------- list[dict] { 'gzh': { 'headimage': str, # 公众号头像 'wechat_name': str, # 公众号名称 }, 'article': { 'url': str, # 文章临时链接 'title': str, # 文章标题 'abstract': str, # 文章摘要 'time': int, # 推送时间,10位时间戳 'open_id': str, # open id 'main_img': str # 封面图片 } } """ page = etree.HTML(text) lis = page.xpath('/html/body/li') gzh_article_list = [] for li in lis: url = get_first_of_element(li, 'div[1]/h4/a/@href') title = get_first_of_element(li, 'div[1]/h4/a/div/text()') abstract = get_first_of_element(li, 'div[1]/p[1]/text()') xpath_time = get_first_of_element(li, 'div[1]/p[2]') open_id = get_first_of_element(xpath_time, 'span/@data-openid') headimage = get_first_of_element(xpath_time, 'span/@data-headimage') gzh_name = get_first_of_element(xpath_time, 'span/text()') send_time = xpath_time.xpath('a/span/@data-lastmodified') main_img = get_first_of_element(li, 'div[2]/a/img/@src') try: send_time = int(send_time[0]) except ValueError: send_time = send_time[0] gzh_article_list.append({ 'gzh': { 'headimage': headimage, 'wechat_name': gzh_name, }, 'article': { 'url': url, 'title': title, 'abstract': abstract, 'time': send_time, 'open_id': open_id, 'main_img': main_img } }) return gzh_article_list
[ "从", "首页热门搜索", "提取公众号信息", "和", "文章列表信息" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/structuring.py#L381-L441
[ "def", "get_gzh_article_by_hot", "(", "text", ")", ":", "page", "=", "etree", ".", "HTML", "(", "text", ")", "lis", "=", "page", ".", "xpath", "(", "'/html/body/li'", ")", "gzh_article_list", "=", "[", "]", "for", "li", "in", "lis", ":", "url", "=", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
WechatSogouStructuring.get_article_detail
根据微信文章的临时链接获取明细 1. 获取文本中所有的图片链接列表 2. 获取微信文章的html内容页面(去除标题等信息) Parameters ---------- text : str or unicode 一篇微信文章的文本 del_qqmusic: bool 删除文章中的qq音乐 del_voice: bool 删除文章中的语音内容 Returns ------- dict { 'content_html': str # 微信文本内容 'content_img_list': list[img_url1, img_url2, ...] # 微信文本中图片列表 }
wechatsogou/structuring.py
def get_article_detail(text, del_qqmusic=True, del_voice=True): """根据微信文章的临时链接获取明细 1. 获取文本中所有的图片链接列表 2. 获取微信文章的html内容页面(去除标题等信息) Parameters ---------- text : str or unicode 一篇微信文章的文本 del_qqmusic: bool 删除文章中的qq音乐 del_voice: bool 删除文章中的语音内容 Returns ------- dict { 'content_html': str # 微信文本内容 'content_img_list': list[img_url1, img_url2, ...] # 微信文本中图片列表 } """ # 1. 获取微信文本content html_obj = BeautifulSoup(text, "lxml") content_text = html_obj.find('div', {'class': 'rich_media_content', 'id': 'js_content'}) # 2. 删除部分标签 if del_qqmusic: qqmusic = content_text.find_all('qqmusic') or [] for music in qqmusic: music.parent.decompose() if del_voice: # voice是一个p标签下的mpvoice标签以及class为'js_audio_frame db'的span构成,所以将父标签删除 voices = content_text.find_all('mpvoice') or [] for voice in voices: voice.parent.decompose() # 3. 获取所有的图片 [img标签,和style中的background-image] all_img_set = set() all_img_element = content_text.find_all('img') or [] for ele in all_img_element: # 删除部分属性 img_url = format_image_url(ele.attrs['data-src']) del ele.attrs['data-src'] ele.attrs['src'] = img_url if not img_url.startswith('http'): raise WechatSogouException('img_url [{}] 不合法'.format(img_url)) all_img_set.add(img_url) backgroud_image = content_text.find_all(style=re.compile("background-image")) or [] for ele in backgroud_image: # 删除部分属性 if ele.attrs.get('data-src'): del ele.attrs['data-src'] if ele.attrs.get('data-wxurl'): del ele.attrs['data-wxurl'] img_url = re.findall(backgroud_image_p, str(ele)) if not img_url: continue all_img_set.add(img_url[0]) # 4. 处理iframe all_img_element = content_text.find_all('iframe') or [] for ele in all_img_element: # 删除部分属性 img_url = ele.attrs['data-src'] del ele.attrs['data-src'] ele.attrs['src'] = img_url # 5. 返回数据 all_img_list = list(all_img_set) content_html = content_text.prettify() # 去除div[id=js_content] content_html = re.findall(js_content, content_html)[0][0] return { 'content_html': content_html, 'content_img_list': all_img_list }
def get_article_detail(text, del_qqmusic=True, del_voice=True): """根据微信文章的临时链接获取明细 1. 获取文本中所有的图片链接列表 2. 获取微信文章的html内容页面(去除标题等信息) Parameters ---------- text : str or unicode 一篇微信文章的文本 del_qqmusic: bool 删除文章中的qq音乐 del_voice: bool 删除文章中的语音内容 Returns ------- dict { 'content_html': str # 微信文本内容 'content_img_list': list[img_url1, img_url2, ...] # 微信文本中图片列表 } """ # 1. 获取微信文本content html_obj = BeautifulSoup(text, "lxml") content_text = html_obj.find('div', {'class': 'rich_media_content', 'id': 'js_content'}) # 2. 删除部分标签 if del_qqmusic: qqmusic = content_text.find_all('qqmusic') or [] for music in qqmusic: music.parent.decompose() if del_voice: # voice是一个p标签下的mpvoice标签以及class为'js_audio_frame db'的span构成,所以将父标签删除 voices = content_text.find_all('mpvoice') or [] for voice in voices: voice.parent.decompose() # 3. 获取所有的图片 [img标签,和style中的background-image] all_img_set = set() all_img_element = content_text.find_all('img') or [] for ele in all_img_element: # 删除部分属性 img_url = format_image_url(ele.attrs['data-src']) del ele.attrs['data-src'] ele.attrs['src'] = img_url if not img_url.startswith('http'): raise WechatSogouException('img_url [{}] 不合法'.format(img_url)) all_img_set.add(img_url) backgroud_image = content_text.find_all(style=re.compile("background-image")) or [] for ele in backgroud_image: # 删除部分属性 if ele.attrs.get('data-src'): del ele.attrs['data-src'] if ele.attrs.get('data-wxurl'): del ele.attrs['data-wxurl'] img_url = re.findall(backgroud_image_p, str(ele)) if not img_url: continue all_img_set.add(img_url[0]) # 4. 处理iframe all_img_element = content_text.find_all('iframe') or [] for ele in all_img_element: # 删除部分属性 img_url = ele.attrs['data-src'] del ele.attrs['data-src'] ele.attrs['src'] = img_url # 5. 返回数据 all_img_list = list(all_img_set) content_html = content_text.prettify() # 去除div[id=js_content] content_html = re.findall(js_content, content_html)[0][0] return { 'content_html': content_html, 'content_img_list': all_img_list }
[ "根据微信文章的临时链接获取明细" ]
Chyroc/WechatSogou
python
https://github.com/Chyroc/WechatSogou/blob/2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a/wechatsogou/structuring.py#L444-L527
[ "def", "get_article_detail", "(", "text", ",", "del_qqmusic", "=", "True", ",", "del_voice", "=", "True", ")", ":", "# 1. 获取微信文本content", "html_obj", "=", "BeautifulSoup", "(", "text", ",", "\"lxml\"", ")", "content_text", "=", "html_obj", ".", "find", "(", ...
2e0e9886f555fd8bcfc7ae9718ced6ce955cd24a
train
_decode_image
Reads and decodes an image from a file object as a Numpy array. The SUN dataset contains images in several formats (despite the fact that all of them have .jpg extension). Some of them are: - BMP (RGB) - PNG (grayscale, RGBA, RGB interlaced) - JPEG (RGB) - GIF (1-frame RGB) Since TFDS assumes that all images have the same number of channels, we convert all of them to RGB. Args: fobj: File object to read from. session: TF session used to decode the images. filename: Filename of the original image in the archive. Returns: Numpy array with shape (height, width, channels).
tensorflow_datasets/image/sun.py
def _decode_image(fobj, session, filename): """Reads and decodes an image from a file object as a Numpy array. The SUN dataset contains images in several formats (despite the fact that all of them have .jpg extension). Some of them are: - BMP (RGB) - PNG (grayscale, RGBA, RGB interlaced) - JPEG (RGB) - GIF (1-frame RGB) Since TFDS assumes that all images have the same number of channels, we convert all of them to RGB. Args: fobj: File object to read from. session: TF session used to decode the images. filename: Filename of the original image in the archive. Returns: Numpy array with shape (height, width, channels). """ buf = fobj.read() image = tfds.core.lazy_imports.cv2.imdecode( np.fromstring(buf, dtype=np.uint8), flags=3) # Note: Converts to RGB. if image is None: logging.warning( "Image %s could not be decoded by OpenCV, falling back to TF", filename) try: image = tf.image.decode_image(buf, channels=3) image = session.run(image) except tf.errors.InvalidArgumentError: logging.fatal("Image %s could not be decoded by Tensorflow", filename) # The GIF images contain a single frame. if len(image.shape) == 4: # rank=4 -> rank=3 image = image.reshape(image.shape[1:]) return image
def _decode_image(fobj, session, filename): """Reads and decodes an image from a file object as a Numpy array. The SUN dataset contains images in several formats (despite the fact that all of them have .jpg extension). Some of them are: - BMP (RGB) - PNG (grayscale, RGBA, RGB interlaced) - JPEG (RGB) - GIF (1-frame RGB) Since TFDS assumes that all images have the same number of channels, we convert all of them to RGB. Args: fobj: File object to read from. session: TF session used to decode the images. filename: Filename of the original image in the archive. Returns: Numpy array with shape (height, width, channels). """ buf = fobj.read() image = tfds.core.lazy_imports.cv2.imdecode( np.fromstring(buf, dtype=np.uint8), flags=3) # Note: Converts to RGB. if image is None: logging.warning( "Image %s could not be decoded by OpenCV, falling back to TF", filename) try: image = tf.image.decode_image(buf, channels=3) image = session.run(image) except tf.errors.InvalidArgumentError: logging.fatal("Image %s could not be decoded by Tensorflow", filename) # The GIF images contain a single frame. if len(image.shape) == 4: # rank=4 -> rank=3 image = image.reshape(image.shape[1:]) return image
[ "Reads", "and", "decodes", "an", "image", "from", "a", "file", "object", "as", "a", "Numpy", "array", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/sun.py#L65-L102
[ "def", "_decode_image", "(", "fobj", ",", "session", ",", "filename", ")", ":", "buf", "=", "fobj", ".", "read", "(", ")", "image", "=", "tfds", ".", "core", ".", "lazy_imports", ".", "cv2", ".", "imdecode", "(", "np", ".", "fromstring", "(", "buf", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_process_image_file
Process image files from the dataset.
tensorflow_datasets/image/sun.py
def _process_image_file(fobj, session, filename): """Process image files from the dataset.""" # We need to read the image files and convert them to JPEG, since some files # actually contain GIF, PNG or BMP data (despite having a .jpg extension) and # some encoding options that will make TF crash in general. image = _decode_image(fobj, session, filename=filename) return _encode_jpeg(image)
def _process_image_file(fobj, session, filename): """Process image files from the dataset.""" # We need to read the image files and convert them to JPEG, since some files # actually contain GIF, PNG or BMP data (despite having a .jpg extension) and # some encoding options that will make TF crash in general. image = _decode_image(fobj, session, filename=filename) return _encode_jpeg(image)
[ "Process", "image", "files", "from", "the", "dataset", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/sun.py#L113-L119
[ "def", "_process_image_file", "(", "fobj", ",", "session", ",", "filename", ")", ":", "# We need to read the image files and convert them to JPEG, since some files", "# actually contain GIF, PNG or BMP data (despite having a .jpg extension) and", "# some encoding options that will make TF cr...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
Sun397._generate_examples
Yields examples.
tensorflow_datasets/image/sun.py
def _generate_examples(self, archive): """Yields examples.""" prefix_len = len("SUN397") with tf.Graph().as_default(): with utils.nogpu_session() as sess: for filepath, fobj in archive: if (filepath.endswith(".jpg") and filepath not in _SUN397_IGNORE_IMAGES): # Note: all files in the tar.gz are in SUN397/... filename = filepath[prefix_len:] # Example: # From filename: /c/car_interior/backseat/sun_aenygxwhhmjtisnf.jpg # To class: /c/car_interior/backseat label = "/".join(filename.split("/")[:-1]) image = _process_image_file(fobj, sess, filepath) yield { "file_name": filename, "image": image, "label": label, }
def _generate_examples(self, archive): """Yields examples.""" prefix_len = len("SUN397") with tf.Graph().as_default(): with utils.nogpu_session() as sess: for filepath, fobj in archive: if (filepath.endswith(".jpg") and filepath not in _SUN397_IGNORE_IMAGES): # Note: all files in the tar.gz are in SUN397/... filename = filepath[prefix_len:] # Example: # From filename: /c/car_interior/backseat/sun_aenygxwhhmjtisnf.jpg # To class: /c/car_interior/backseat label = "/".join(filename.split("/")[:-1]) image = _process_image_file(fobj, sess, filepath) yield { "file_name": filename, "image": image, "label": label, }
[ "Yields", "examples", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/sun.py#L157-L176
[ "def", "_generate_examples", "(", "self", ",", "archive", ")", ":", "prefix_len", "=", "len", "(", "\"SUN397\"", ")", "with", "tf", ".", "Graph", "(", ")", ".", "as_default", "(", ")", ":", "with", "utils", ".", "nogpu_session", "(", ")", "as", "sess",...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_parse_parallel_sentences
Returns examples from parallel SGML or text files, which may be gzipped.
tensorflow_datasets/translate/wmt.py
def _parse_parallel_sentences(f1, f2): """Returns examples from parallel SGML or text files, which may be gzipped.""" def _parse_text(path): """Returns the sentences from a single text file, which may be gzipped.""" split_path = path.split(".") if split_path[-1] == "gz": lang = split_path[-2] with tf.io.gfile.GFile(path) as f, gzip.GzipFile(fileobj=f) as g: return g.read().split("\n"), lang if split_path[-1] == "txt": # CWMT lang = split_path[-2].split("_")[-1] lang = "zh" if lang in ("ch", "cn") else lang else: lang = split_path[-1] with tf.io.gfile.GFile(path) as f: return f.read().split("\n"), lang def _parse_sgm(path): """Returns sentences from a single SGML file.""" lang = path.split(".")[-2] sentences = [] # Note: We can't use the XML parser since some of the files are badly # formatted. seg_re = re.compile(r"<seg id=\"\d+\">(.*)</seg>") with tf.io.gfile.GFile(path) as f: for line in f: seg_match = re.match(seg_re, line) if seg_match: assert len(seg_match.groups()) == 1 sentences.append(seg_match.groups()[0]) return sentences, lang parse_file = _parse_sgm if f1.endswith(".sgm") else _parse_text # Some datasets (e.g., CWMT) contain multiple parallel files specified with # a wildcard. We sort both sets to align them and parse them one by one. f1_files = tf.io.gfile.glob(f1) f2_files = tf.io.gfile.glob(f2) assert f1_files and f2_files, "No matching files found: %s, %s." % (f1, f2) assert len(f1_files) == len(f2_files), ( "Number of files do not match: %d vs %d for %s vs %s." % ( len(f1_files), len(f2_files), f1, f2)) for f1_i, f2_i in zip(sorted(f1_files), sorted(f2_files)): l1_sentences, l1 = parse_file(f1_i) l2_sentences, l2 = parse_file(f2_i) assert len(l1_sentences) == len(l2_sentences), ( "Sizes do not match: %d vs %d for %s vs %s." % ( len(l1_sentences), len(l2_sentences), f1_i, f2_i)) for s1, s2 in zip(l1_sentences, l2_sentences): yield { l1: s1, l2: s2 }
def _parse_parallel_sentences(f1, f2): """Returns examples from parallel SGML or text files, which may be gzipped.""" def _parse_text(path): """Returns the sentences from a single text file, which may be gzipped.""" split_path = path.split(".") if split_path[-1] == "gz": lang = split_path[-2] with tf.io.gfile.GFile(path) as f, gzip.GzipFile(fileobj=f) as g: return g.read().split("\n"), lang if split_path[-1] == "txt": # CWMT lang = split_path[-2].split("_")[-1] lang = "zh" if lang in ("ch", "cn") else lang else: lang = split_path[-1] with tf.io.gfile.GFile(path) as f: return f.read().split("\n"), lang def _parse_sgm(path): """Returns sentences from a single SGML file.""" lang = path.split(".")[-2] sentences = [] # Note: We can't use the XML parser since some of the files are badly # formatted. seg_re = re.compile(r"<seg id=\"\d+\">(.*)</seg>") with tf.io.gfile.GFile(path) as f: for line in f: seg_match = re.match(seg_re, line) if seg_match: assert len(seg_match.groups()) == 1 sentences.append(seg_match.groups()[0]) return sentences, lang parse_file = _parse_sgm if f1.endswith(".sgm") else _parse_text # Some datasets (e.g., CWMT) contain multiple parallel files specified with # a wildcard. We sort both sets to align them and parse them one by one. f1_files = tf.io.gfile.glob(f1) f2_files = tf.io.gfile.glob(f2) assert f1_files and f2_files, "No matching files found: %s, %s." % (f1, f2) assert len(f1_files) == len(f2_files), ( "Number of files do not match: %d vs %d for %s vs %s." % ( len(f1_files), len(f2_files), f1, f2)) for f1_i, f2_i in zip(sorted(f1_files), sorted(f2_files)): l1_sentences, l1 = parse_file(f1_i) l2_sentences, l2 = parse_file(f2_i) assert len(l1_sentences) == len(l2_sentences), ( "Sizes do not match: %d vs %d for %s vs %s." % ( len(l1_sentences), len(l2_sentences), f1_i, f2_i)) for s1, s2 in zip(l1_sentences, l2_sentences): yield { l1: s1, l2: s2 }
[ "Returns", "examples", "from", "parallel", "SGML", "or", "text", "files", "which", "may", "be", "gzipped", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/wmt.py#L761-L820
[ "def", "_parse_parallel_sentences", "(", "f1", ",", "f2", ")", ":", "def", "_parse_text", "(", "path", ")", ":", "\"\"\"Returns the sentences from a single text file, which may be gzipped.\"\"\"", "split_path", "=", "path", ".", "split", "(", "\".\"", ")", "if", "spli...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_parse_tmx
Generates examples from TMX file.
tensorflow_datasets/translate/wmt.py
def _parse_tmx(path): """Generates examples from TMX file.""" def _get_tuv_lang(tuv): for k, v in tuv.items(): if k.endswith("}lang"): return v raise AssertionError("Language not found in `tuv` attributes.") def _get_tuv_seg(tuv): segs = tuv.findall("seg") assert len(segs) == 1, "Invalid number of segments: %d" % len(segs) return segs[0].text with tf.io.gfile.GFile(path) as f: for _, elem in ElementTree.iterparse(f): if elem.tag == "tu": yield { _get_tuv_lang(tuv): _get_tuv_seg(tuv) for tuv in elem.iterfind("tuv") } elem.clear()
def _parse_tmx(path): """Generates examples from TMX file.""" def _get_tuv_lang(tuv): for k, v in tuv.items(): if k.endswith("}lang"): return v raise AssertionError("Language not found in `tuv` attributes.") def _get_tuv_seg(tuv): segs = tuv.findall("seg") assert len(segs) == 1, "Invalid number of segments: %d" % len(segs) return segs[0].text with tf.io.gfile.GFile(path) as f: for _, elem in ElementTree.iterparse(f): if elem.tag == "tu": yield { _get_tuv_lang(tuv): _get_tuv_seg(tuv) for tuv in elem.iterfind("tuv") } elem.clear()
[ "Generates", "examples", "from", "TMX", "file", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/wmt.py#L838-L858
[ "def", "_parse_tmx", "(", "path", ")", ":", "def", "_get_tuv_lang", "(", "tuv", ")", ":", "for", "k", ",", "v", "in", "tuv", ".", "items", "(", ")", ":", "if", "k", ".", "endswith", "(", "\"}lang\"", ")", ":", "return", "v", "raise", "AssertionErro...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_parse_tsv
Generates examples from TSV file.
tensorflow_datasets/translate/wmt.py
def _parse_tsv(path, language_pair=None): """Generates examples from TSV file.""" if language_pair is None: lang_match = re.match(r".*\.([a-z][a-z])-([a-z][a-z])\.tsv", path) assert lang_match is not None, "Invalid TSV filename: %s" % path l1, l2 = lang_match.groups() else: l1, l2 = language_pair with tf.io.gfile.GFile(path) as f: for j, line in enumerate(f): cols = line.split("\t") if len(cols) != 2: logging.warning( "Skipping line %d in TSV (%s) with %d != 2 columns.", j, path, len(cols)) continue s1, s2 = cols yield { l1: s1.strip(), l2: s2.strip() }
def _parse_tsv(path, language_pair=None): """Generates examples from TSV file.""" if language_pair is None: lang_match = re.match(r".*\.([a-z][a-z])-([a-z][a-z])\.tsv", path) assert lang_match is not None, "Invalid TSV filename: %s" % path l1, l2 = lang_match.groups() else: l1, l2 = language_pair with tf.io.gfile.GFile(path) as f: for j, line in enumerate(f): cols = line.split("\t") if len(cols) != 2: logging.warning( "Skipping line %d in TSV (%s) with %d != 2 columns.", j, path, len(cols)) continue s1, s2 = cols yield { l1: s1.strip(), l2: s2.strip() }
[ "Generates", "examples", "from", "TSV", "file", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/wmt.py#L861-L881
[ "def", "_parse_tsv", "(", "path", ",", "language_pair", "=", "None", ")", ":", "if", "language_pair", "is", "None", ":", "lang_match", "=", "re", ".", "match", "(", "r\".*\\.([a-z][a-z])-([a-z][a-z])\\.tsv\"", ",", "path", ")", "assert", "lang_match", "is", "n...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_parse_wikiheadlines
Generates examples from Wikiheadlines dataset file.
tensorflow_datasets/translate/wmt.py
def _parse_wikiheadlines(path): """Generates examples from Wikiheadlines dataset file.""" lang_match = re.match(r".*\.([a-z][a-z])-([a-z][a-z])$", path) assert lang_match is not None, "Invalid Wikiheadlines filename: %s" % path l1, l2 = lang_match.groups() with tf.io.gfile.GFile(path) as f: for line in f: s1, s2 = line.split("|||") yield { l1: s1.strip(), l2: s2.strip() }
def _parse_wikiheadlines(path): """Generates examples from Wikiheadlines dataset file.""" lang_match = re.match(r".*\.([a-z][a-z])-([a-z][a-z])$", path) assert lang_match is not None, "Invalid Wikiheadlines filename: %s" % path l1, l2 = lang_match.groups() with tf.io.gfile.GFile(path) as f: for line in f: s1, s2 = line.split("|||") yield { l1: s1.strip(), l2: s2.strip() }
[ "Generates", "examples", "from", "Wikiheadlines", "dataset", "file", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/wmt.py#L884-L895
[ "def", "_parse_wikiheadlines", "(", "path", ")", ":", "lang_match", "=", "re", ".", "match", "(", "r\".*\\.([a-z][a-z])-([a-z][a-z])$\"", ",", "path", ")", "assert", "lang_match", "is", "not", "None", ",", "\"Invalid Wikiheadlines filename: %s\"", "%", "path", "l1",...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_parse_czeng
Generates examples from CzEng v1.6, with optional filtering for v1.7.
tensorflow_datasets/translate/wmt.py
def _parse_czeng(*paths, **kwargs): """Generates examples from CzEng v1.6, with optional filtering for v1.7.""" filter_path = kwargs.get("filter_path", None) if filter_path: re_block = re.compile(r"^[^-]+-b(\d+)-\d\d[tde]") with tf.io.gfile.GFile(filter_path) as f: bad_blocks = { blk for blk in re.search( r"qw{([\s\d]*)}", f.read()).groups()[0].split() } logging.info( "Loaded %d bad blocks to filter from CzEng v1.6 to make v1.7.", len(bad_blocks)) for path in paths: for gz_path in tf.io.gfile.glob(path): with tf.io.gfile.GFile(gz_path, "rb") as g, gzip.GzipFile(fileobj=g) as f: for line in f: line = line.decode("utf-8") # required for py3 if not line.strip(): continue id_, unused_score, cs, en = line.split("\t") if filter_path: block_match = re.match(re_block, id_) if block_match and block_match.groups()[0] in bad_blocks: continue yield { "cs": cs.strip(), "en": en.strip(), }
def _parse_czeng(*paths, **kwargs): """Generates examples from CzEng v1.6, with optional filtering for v1.7.""" filter_path = kwargs.get("filter_path", None) if filter_path: re_block = re.compile(r"^[^-]+-b(\d+)-\d\d[tde]") with tf.io.gfile.GFile(filter_path) as f: bad_blocks = { blk for blk in re.search( r"qw{([\s\d]*)}", f.read()).groups()[0].split() } logging.info( "Loaded %d bad blocks to filter from CzEng v1.6 to make v1.7.", len(bad_blocks)) for path in paths: for gz_path in tf.io.gfile.glob(path): with tf.io.gfile.GFile(gz_path, "rb") as g, gzip.GzipFile(fileobj=g) as f: for line in f: line = line.decode("utf-8") # required for py3 if not line.strip(): continue id_, unused_score, cs, en = line.split("\t") if filter_path: block_match = re.match(re_block, id_) if block_match and block_match.groups()[0] in bad_blocks: continue yield { "cs": cs.strip(), "en": en.strip(), }
[ "Generates", "examples", "from", "CzEng", "v1", ".", "6", "with", "optional", "filtering", "for", "v1", ".", "7", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/wmt.py#L898-L927
[ "def", "_parse_czeng", "(", "*", "paths", ",", "*", "*", "kwargs", ")", ":", "filter_path", "=", "kwargs", ".", "get", "(", "\"filter_path\"", ",", "None", ")", "if", "filter_path", ":", "re_block", "=", "re", ".", "compile", "(", "r\"^[^-]+-b(\\d+)-\\d\\d...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SubDataset._inject_language
Injects languages into (potentially) template strings.
tensorflow_datasets/translate/wmt.py
def _inject_language(self, src, strings): """Injects languages into (potentially) template strings.""" if src not in self.sources: raise ValueError("Invalid source for '{0}': {1}".format(self.name, src)) def _format_string(s): if "{0}" in s and "{1}" and "{src}" in s: return s.format(*sorted([src, self.target]), src=src) elif "{0}" in s and "{1}" in s: return s.format(*sorted([src, self.target])) elif "{src}" in s: return s.format(src=src) else: return s return [_format_string(s) for s in strings]
def _inject_language(self, src, strings): """Injects languages into (potentially) template strings.""" if src not in self.sources: raise ValueError("Invalid source for '{0}': {1}".format(self.name, src)) def _format_string(s): if "{0}" in s and "{1}" and "{src}" in s: return s.format(*sorted([src, self.target]), src=src) elif "{0}" in s and "{1}" in s: return s.format(*sorted([src, self.target])) elif "{src}" in s: return s.format(src=src) else: return s return [_format_string(s) for s in strings]
[ "Injects", "languages", "into", "(", "potentially", ")", "template", "strings", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/wmt.py#L97-L110
[ "def", "_inject_language", "(", "self", ",", "src", ",", "strings", ")", ":", "if", "src", "not", "in", "self", ".", "sources", ":", "raise", "ValueError", "(", "\"Invalid source for '{0}': {1}\"", ".", "format", "(", "self", ".", "name", ",", "src", ")", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
WmtTranslate.subsets
Subsets that make up each split of the dataset for the language pair.
tensorflow_datasets/translate/wmt.py
def subsets(self): """Subsets that make up each split of the dataset for the language pair.""" source, target = self.builder_config.language_pair filtered_subsets = {} for split, ss_names in self._subsets.items(): filtered_subsets[split] = [] for ss_name in ss_names: ds = DATASET_MAP[ss_name] if ds.target != target or source not in ds.sources: logging.info( "Skipping sub-dataset that does not include language pair: %s", ss_name) else: filtered_subsets[split].append(ss_name) logging.info("Using sub-datasets: %s", filtered_subsets) return filtered_subsets
def subsets(self): """Subsets that make up each split of the dataset for the language pair.""" source, target = self.builder_config.language_pair filtered_subsets = {} for split, ss_names in self._subsets.items(): filtered_subsets[split] = [] for ss_name in ss_names: ds = DATASET_MAP[ss_name] if ds.target != target or source not in ds.sources: logging.info( "Skipping sub-dataset that does not include language pair: %s", ss_name) else: filtered_subsets[split].append(ss_name) logging.info("Using sub-datasets: %s", filtered_subsets) return filtered_subsets
[ "Subsets", "that", "make", "up", "each", "split", "of", "the", "dataset", "for", "the", "language", "pair", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/wmt.py#L615-L630
[ "def", "subsets", "(", "self", ")", ":", "source", ",", "target", "=", "self", ".", "builder_config", ".", "language_pair", "filtered_subsets", "=", "{", "}", "for", "split", ",", "ss_names", "in", "self", ".", "_subsets", ".", "items", "(", ")", ":", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
WmtTranslate._generate_examples
Returns the examples in the raw (text) form.
tensorflow_datasets/translate/wmt.py
def _generate_examples(self, split_subsets, extraction_map): """Returns the examples in the raw (text) form.""" source, _ = self.builder_config.language_pair def _get_local_paths(ds, extract_dirs): rel_paths = ds.get_path(source) if len(extract_dirs) == 1: extract_dirs = extract_dirs * len(rel_paths) return [os.path.join(ex_dir, rel_path) if rel_path else ex_dir for ex_dir, rel_path in zip(extract_dirs, rel_paths)] for ss_name in split_subsets: logging.info("Generating examples from: %s", ss_name) ds = DATASET_MAP[ss_name] extract_dirs = extraction_map[ss_name] files = _get_local_paths(ds, extract_dirs) if ss_name.startswith("czeng"): if ss_name.endswith("16pre"): sub_generator = functools.partial( _parse_tsv, language_pair=("en", "cs")) elif ss_name.endswith("17"): filter_path = _get_local_paths( _CZENG17_FILTER, extraction_map[_CZENG17_FILTER.name])[0] sub_generator = functools.partial( _parse_czeng, filter_path=filter_path) else: sub_generator = _parse_czeng elif len(files) == 2: if ss_name.endswith("_frde"): sub_generator = _parse_frde_bitext else: sub_generator = _parse_parallel_sentences elif len(files) == 1: fname = files[0] # Note: Due to formatting used by `download_manager`, the file # extension may not be at the end of the file path. if ".tsv" in fname: sub_generator = _parse_tsv elif ss_name.startswith("newscommentary_v14"): sub_generator = functools.partial( _parse_tsv, language_pair=self.builder_config.language_pair) elif "tmx" in fname: sub_generator = _parse_tmx elif ss_name.startswith("wikiheadlines"): sub_generator = _parse_wikiheadlines else: raise ValueError("Unsupported file format: %s" % fname) else: raise ValueError("Invalid number of files: %d" % len(files)) for ex in sub_generator(*files): if not all(ex.values()): continue # TODO(adarob): Add subset feature. # ex["subset"] = subset yield ex
def _generate_examples(self, split_subsets, extraction_map): """Returns the examples in the raw (text) form.""" source, _ = self.builder_config.language_pair def _get_local_paths(ds, extract_dirs): rel_paths = ds.get_path(source) if len(extract_dirs) == 1: extract_dirs = extract_dirs * len(rel_paths) return [os.path.join(ex_dir, rel_path) if rel_path else ex_dir for ex_dir, rel_path in zip(extract_dirs, rel_paths)] for ss_name in split_subsets: logging.info("Generating examples from: %s", ss_name) ds = DATASET_MAP[ss_name] extract_dirs = extraction_map[ss_name] files = _get_local_paths(ds, extract_dirs) if ss_name.startswith("czeng"): if ss_name.endswith("16pre"): sub_generator = functools.partial( _parse_tsv, language_pair=("en", "cs")) elif ss_name.endswith("17"): filter_path = _get_local_paths( _CZENG17_FILTER, extraction_map[_CZENG17_FILTER.name])[0] sub_generator = functools.partial( _parse_czeng, filter_path=filter_path) else: sub_generator = _parse_czeng elif len(files) == 2: if ss_name.endswith("_frde"): sub_generator = _parse_frde_bitext else: sub_generator = _parse_parallel_sentences elif len(files) == 1: fname = files[0] # Note: Due to formatting used by `download_manager`, the file # extension may not be at the end of the file path. if ".tsv" in fname: sub_generator = _parse_tsv elif ss_name.startswith("newscommentary_v14"): sub_generator = functools.partial( _parse_tsv, language_pair=self.builder_config.language_pair) elif "tmx" in fname: sub_generator = _parse_tmx elif ss_name.startswith("wikiheadlines"): sub_generator = _parse_wikiheadlines else: raise ValueError("Unsupported file format: %s" % fname) else: raise ValueError("Invalid number of files: %d" % len(files)) for ex in sub_generator(*files): if not all(ex.values()): continue # TODO(adarob): Add subset feature. # ex["subset"] = subset yield ex
[ "Returns", "the", "examples", "in", "the", "raw", "(", "text", ")", "form", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/translate/wmt.py#L703-L758
[ "def", "_generate_examples", "(", "self", ",", "split_subsets", ",", "extraction_map", ")", ":", "source", ",", "_", "=", "self", ".", "builder_config", ".", "language_pair", "def", "_get_local_paths", "(", "ds", ",", "extract_dirs", ")", ":", "rel_paths", "="...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
builder
Fetches a `tfds.core.DatasetBuilder` by string name. Args: name: `str`, the registered name of the `DatasetBuilder` (the snake case version of the class name). This can be either `"dataset_name"` or `"dataset_name/config_name"` for datasets with `BuilderConfig`s. As a convenience, this string may contain comma-separated keyword arguments for the builder. For example `"foo_bar/a=True,b=3"` would use the `FooBar` dataset passing the keyword arguments `a=True` and `b=3` (for builders with configs, it would be `"foo_bar/zoo/a=True,b=3"` to use the `"zoo"` config and pass to the builder keyword arguments `a=True` and `b=3`). **builder_init_kwargs: `dict` of keyword arguments passed to the `DatasetBuilder`. These will override keyword arguments passed in `name`, if any. Returns: A `tfds.core.DatasetBuilder`. Raises: DatasetNotFoundError: if `name` is unrecognized.
tensorflow_datasets/core/registered.py
def builder(name, **builder_init_kwargs): """Fetches a `tfds.core.DatasetBuilder` by string name. Args: name: `str`, the registered name of the `DatasetBuilder` (the snake case version of the class name). This can be either `"dataset_name"` or `"dataset_name/config_name"` for datasets with `BuilderConfig`s. As a convenience, this string may contain comma-separated keyword arguments for the builder. For example `"foo_bar/a=True,b=3"` would use the `FooBar` dataset passing the keyword arguments `a=True` and `b=3` (for builders with configs, it would be `"foo_bar/zoo/a=True,b=3"` to use the `"zoo"` config and pass to the builder keyword arguments `a=True` and `b=3`). **builder_init_kwargs: `dict` of keyword arguments passed to the `DatasetBuilder`. These will override keyword arguments passed in `name`, if any. Returns: A `tfds.core.DatasetBuilder`. Raises: DatasetNotFoundError: if `name` is unrecognized. """ name, builder_kwargs = _dataset_name_and_kwargs_from_name_str(name) builder_kwargs.update(builder_init_kwargs) if name in _ABSTRACT_DATASET_REGISTRY: raise DatasetNotFoundError(name, is_abstract=True) if name in _IN_DEVELOPMENT_REGISTRY: raise DatasetNotFoundError(name, in_development=True) if name not in _DATASET_REGISTRY: raise DatasetNotFoundError(name) try: return _DATASET_REGISTRY[name](**builder_kwargs) except BaseException: logging.error("Failed to construct dataset %s", name) raise
def builder(name, **builder_init_kwargs): """Fetches a `tfds.core.DatasetBuilder` by string name. Args: name: `str`, the registered name of the `DatasetBuilder` (the snake case version of the class name). This can be either `"dataset_name"` or `"dataset_name/config_name"` for datasets with `BuilderConfig`s. As a convenience, this string may contain comma-separated keyword arguments for the builder. For example `"foo_bar/a=True,b=3"` would use the `FooBar` dataset passing the keyword arguments `a=True` and `b=3` (for builders with configs, it would be `"foo_bar/zoo/a=True,b=3"` to use the `"zoo"` config and pass to the builder keyword arguments `a=True` and `b=3`). **builder_init_kwargs: `dict` of keyword arguments passed to the `DatasetBuilder`. These will override keyword arguments passed in `name`, if any. Returns: A `tfds.core.DatasetBuilder`. Raises: DatasetNotFoundError: if `name` is unrecognized. """ name, builder_kwargs = _dataset_name_and_kwargs_from_name_str(name) builder_kwargs.update(builder_init_kwargs) if name in _ABSTRACT_DATASET_REGISTRY: raise DatasetNotFoundError(name, is_abstract=True) if name in _IN_DEVELOPMENT_REGISTRY: raise DatasetNotFoundError(name, in_development=True) if name not in _DATASET_REGISTRY: raise DatasetNotFoundError(name) try: return _DATASET_REGISTRY[name](**builder_kwargs) except BaseException: logging.error("Failed to construct dataset %s", name) raise
[ "Fetches", "a", "tfds", ".", "core", ".", "DatasetBuilder", "by", "string", "name", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/registered.py#L137-L172
[ "def", "builder", "(", "name", ",", "*", "*", "builder_init_kwargs", ")", ":", "name", ",", "builder_kwargs", "=", "_dataset_name_and_kwargs_from_name_str", "(", "name", ")", "builder_kwargs", ".", "update", "(", "builder_init_kwargs", ")", "if", "name", "in", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
load
Loads the named dataset into a `tf.data.Dataset`. If `split=None` (the default), returns all splits for the dataset. Otherwise, returns the specified split. `load` is a convenience method that fetches the `tfds.core.DatasetBuilder` by string name, optionally calls `DatasetBuilder.download_and_prepare` (if `download=True`), and then calls `DatasetBuilder.as_dataset`. This is roughly equivalent to: ``` builder = tfds.builder(name, data_dir=data_dir, **builder_kwargs) if download: builder.download_and_prepare(**download_and_prepare_kwargs) ds = builder.as_dataset( split=split, as_supervised=as_supervised, **as_dataset_kwargs) if with_info: return ds, builder.info return ds ``` If you'd like NumPy arrays instead of `tf.data.Dataset`s or `tf.Tensor`s, you can pass the return value to `tfds.as_numpy`. Callers must pass arguments as keyword arguments. **Warning**: calling this function might potentially trigger the download of hundreds of GiB to disk. Refer to the `download` argument. Args: name: `str`, the registered name of the `DatasetBuilder` (the snake case version of the class name). This can be either `"dataset_name"` or `"dataset_name/config_name"` for datasets with `BuilderConfig`s. As a convenience, this string may contain comma-separated keyword arguments for the builder. For example `"foo_bar/a=True,b=3"` would use the `FooBar` dataset passing the keyword arguments `a=True` and `b=3` (for builders with configs, it would be `"foo_bar/zoo/a=True,b=3"` to use the `"zoo"` config and pass to the builder keyword arguments `a=True` and `b=3`). split: `tfds.Split` or `str`, which split of the data to load. If None, will return a `dict` with all splits (typically `tfds.Split.TRAIN` and `tfds.Split.TEST`). data_dir: `str` (optional), directory to read/write data. Defaults to "~/tensorflow_datasets". batch_size: `int`, set to > 1 to get batches of examples. Note that variable length features will be 0-padded. If `batch_size=-1`, will return the full dataset as `tf.Tensor`s. download: `bool` (optional), whether to call `tfds.core.DatasetBuilder.download_and_prepare` before calling `tf.DatasetBuilder.as_dataset`. If `False`, data is expected to be in `data_dir`. If `True` and the data is already in `data_dir`, `download_and_prepare` is a no-op. as_supervised: `bool`, if `True`, the returned `tf.data.Dataset` will have a 2-tuple structure `(input, label)` according to `builder.info.supervised_keys`. If `False`, the default, the returned `tf.data.Dataset` will have a dictionary with all the features. with_info: `bool`, if True, tfds.load will return the tuple (tf.data.Dataset, tfds.core.DatasetInfo) containing the info associated with the builder. builder_kwargs: `dict` (optional), keyword arguments to be passed to the `tfds.core.DatasetBuilder` constructor. `data_dir` will be passed through by default. download_and_prepare_kwargs: `dict` (optional) keyword arguments passed to `tfds.core.DatasetBuilder.download_and_prepare` if `download=True`. Allow to control where to download and extract the cached data. If not set, cache_dir and manual_dir will automatically be deduced from data_dir. as_dataset_kwargs: `dict` (optional), keyword arguments passed to `tfds.core.DatasetBuilder.as_dataset`. `split` will be passed through by default. Example: `{'shuffle_files': True}`. Note that shuffle_files is False by default unless `split == tfds.Split.TRAIN`. try_gcs: `bool`, if True, tfds.load will see if the dataset exists on the public GCS bucket before building it locally. Returns: ds: `tf.data.Dataset`, the dataset requested, or if `split` is None, a `dict<key: tfds.Split, value: tfds.data.Dataset>`. If `batch_size=-1`, these will be full datasets as `tf.Tensor`s. ds_info: `tfds.core.DatasetInfo`, if `with_info` is True, then `tfds.load` will return a tuple `(ds, ds_info)` containing dataset information (version, features, splits, num_examples,...). Note that the `ds_info` object documents the entire dataset, regardless of the `split` requested. Split-specific information is available in `ds_info.splits`.
tensorflow_datasets/core/registered.py
def load(name, split=None, data_dir=None, batch_size=1, download=True, as_supervised=False, with_info=False, builder_kwargs=None, download_and_prepare_kwargs=None, as_dataset_kwargs=None, try_gcs=False): """Loads the named dataset into a `tf.data.Dataset`. If `split=None` (the default), returns all splits for the dataset. Otherwise, returns the specified split. `load` is a convenience method that fetches the `tfds.core.DatasetBuilder` by string name, optionally calls `DatasetBuilder.download_and_prepare` (if `download=True`), and then calls `DatasetBuilder.as_dataset`. This is roughly equivalent to: ``` builder = tfds.builder(name, data_dir=data_dir, **builder_kwargs) if download: builder.download_and_prepare(**download_and_prepare_kwargs) ds = builder.as_dataset( split=split, as_supervised=as_supervised, **as_dataset_kwargs) if with_info: return ds, builder.info return ds ``` If you'd like NumPy arrays instead of `tf.data.Dataset`s or `tf.Tensor`s, you can pass the return value to `tfds.as_numpy`. Callers must pass arguments as keyword arguments. **Warning**: calling this function might potentially trigger the download of hundreds of GiB to disk. Refer to the `download` argument. Args: name: `str`, the registered name of the `DatasetBuilder` (the snake case version of the class name). This can be either `"dataset_name"` or `"dataset_name/config_name"` for datasets with `BuilderConfig`s. As a convenience, this string may contain comma-separated keyword arguments for the builder. For example `"foo_bar/a=True,b=3"` would use the `FooBar` dataset passing the keyword arguments `a=True` and `b=3` (for builders with configs, it would be `"foo_bar/zoo/a=True,b=3"` to use the `"zoo"` config and pass to the builder keyword arguments `a=True` and `b=3`). split: `tfds.Split` or `str`, which split of the data to load. If None, will return a `dict` with all splits (typically `tfds.Split.TRAIN` and `tfds.Split.TEST`). data_dir: `str` (optional), directory to read/write data. Defaults to "~/tensorflow_datasets". batch_size: `int`, set to > 1 to get batches of examples. Note that variable length features will be 0-padded. If `batch_size=-1`, will return the full dataset as `tf.Tensor`s. download: `bool` (optional), whether to call `tfds.core.DatasetBuilder.download_and_prepare` before calling `tf.DatasetBuilder.as_dataset`. If `False`, data is expected to be in `data_dir`. If `True` and the data is already in `data_dir`, `download_and_prepare` is a no-op. as_supervised: `bool`, if `True`, the returned `tf.data.Dataset` will have a 2-tuple structure `(input, label)` according to `builder.info.supervised_keys`. If `False`, the default, the returned `tf.data.Dataset` will have a dictionary with all the features. with_info: `bool`, if True, tfds.load will return the tuple (tf.data.Dataset, tfds.core.DatasetInfo) containing the info associated with the builder. builder_kwargs: `dict` (optional), keyword arguments to be passed to the `tfds.core.DatasetBuilder` constructor. `data_dir` will be passed through by default. download_and_prepare_kwargs: `dict` (optional) keyword arguments passed to `tfds.core.DatasetBuilder.download_and_prepare` if `download=True`. Allow to control where to download and extract the cached data. If not set, cache_dir and manual_dir will automatically be deduced from data_dir. as_dataset_kwargs: `dict` (optional), keyword arguments passed to `tfds.core.DatasetBuilder.as_dataset`. `split` will be passed through by default. Example: `{'shuffle_files': True}`. Note that shuffle_files is False by default unless `split == tfds.Split.TRAIN`. try_gcs: `bool`, if True, tfds.load will see if the dataset exists on the public GCS bucket before building it locally. Returns: ds: `tf.data.Dataset`, the dataset requested, or if `split` is None, a `dict<key: tfds.Split, value: tfds.data.Dataset>`. If `batch_size=-1`, these will be full datasets as `tf.Tensor`s. ds_info: `tfds.core.DatasetInfo`, if `with_info` is True, then `tfds.load` will return a tuple `(ds, ds_info)` containing dataset information (version, features, splits, num_examples,...). Note that the `ds_info` object documents the entire dataset, regardless of the `split` requested. Split-specific information is available in `ds_info.splits`. """ name, name_builder_kwargs = _dataset_name_and_kwargs_from_name_str(name) name_builder_kwargs.update(builder_kwargs or {}) builder_kwargs = name_builder_kwargs # Set data_dir if try_gcs and gcs_utils.is_dataset_on_gcs(name): data_dir = constants.GCS_DATA_DIR elif data_dir is None: data_dir = constants.DATA_DIR dbuilder = builder(name, data_dir=data_dir, **builder_kwargs) if download: download_and_prepare_kwargs = download_and_prepare_kwargs or {} dbuilder.download_and_prepare(**download_and_prepare_kwargs) if as_dataset_kwargs is None: as_dataset_kwargs = {} as_dataset_kwargs = dict(as_dataset_kwargs) as_dataset_kwargs["split"] = split as_dataset_kwargs["as_supervised"] = as_supervised as_dataset_kwargs["batch_size"] = batch_size ds = dbuilder.as_dataset(**as_dataset_kwargs) if with_info: return ds, dbuilder.info return ds
def load(name, split=None, data_dir=None, batch_size=1, download=True, as_supervised=False, with_info=False, builder_kwargs=None, download_and_prepare_kwargs=None, as_dataset_kwargs=None, try_gcs=False): """Loads the named dataset into a `tf.data.Dataset`. If `split=None` (the default), returns all splits for the dataset. Otherwise, returns the specified split. `load` is a convenience method that fetches the `tfds.core.DatasetBuilder` by string name, optionally calls `DatasetBuilder.download_and_prepare` (if `download=True`), and then calls `DatasetBuilder.as_dataset`. This is roughly equivalent to: ``` builder = tfds.builder(name, data_dir=data_dir, **builder_kwargs) if download: builder.download_and_prepare(**download_and_prepare_kwargs) ds = builder.as_dataset( split=split, as_supervised=as_supervised, **as_dataset_kwargs) if with_info: return ds, builder.info return ds ``` If you'd like NumPy arrays instead of `tf.data.Dataset`s or `tf.Tensor`s, you can pass the return value to `tfds.as_numpy`. Callers must pass arguments as keyword arguments. **Warning**: calling this function might potentially trigger the download of hundreds of GiB to disk. Refer to the `download` argument. Args: name: `str`, the registered name of the `DatasetBuilder` (the snake case version of the class name). This can be either `"dataset_name"` or `"dataset_name/config_name"` for datasets with `BuilderConfig`s. As a convenience, this string may contain comma-separated keyword arguments for the builder. For example `"foo_bar/a=True,b=3"` would use the `FooBar` dataset passing the keyword arguments `a=True` and `b=3` (for builders with configs, it would be `"foo_bar/zoo/a=True,b=3"` to use the `"zoo"` config and pass to the builder keyword arguments `a=True` and `b=3`). split: `tfds.Split` or `str`, which split of the data to load. If None, will return a `dict` with all splits (typically `tfds.Split.TRAIN` and `tfds.Split.TEST`). data_dir: `str` (optional), directory to read/write data. Defaults to "~/tensorflow_datasets". batch_size: `int`, set to > 1 to get batches of examples. Note that variable length features will be 0-padded. If `batch_size=-1`, will return the full dataset as `tf.Tensor`s. download: `bool` (optional), whether to call `tfds.core.DatasetBuilder.download_and_prepare` before calling `tf.DatasetBuilder.as_dataset`. If `False`, data is expected to be in `data_dir`. If `True` and the data is already in `data_dir`, `download_and_prepare` is a no-op. as_supervised: `bool`, if `True`, the returned `tf.data.Dataset` will have a 2-tuple structure `(input, label)` according to `builder.info.supervised_keys`. If `False`, the default, the returned `tf.data.Dataset` will have a dictionary with all the features. with_info: `bool`, if True, tfds.load will return the tuple (tf.data.Dataset, tfds.core.DatasetInfo) containing the info associated with the builder. builder_kwargs: `dict` (optional), keyword arguments to be passed to the `tfds.core.DatasetBuilder` constructor. `data_dir` will be passed through by default. download_and_prepare_kwargs: `dict` (optional) keyword arguments passed to `tfds.core.DatasetBuilder.download_and_prepare` if `download=True`. Allow to control where to download and extract the cached data. If not set, cache_dir and manual_dir will automatically be deduced from data_dir. as_dataset_kwargs: `dict` (optional), keyword arguments passed to `tfds.core.DatasetBuilder.as_dataset`. `split` will be passed through by default. Example: `{'shuffle_files': True}`. Note that shuffle_files is False by default unless `split == tfds.Split.TRAIN`. try_gcs: `bool`, if True, tfds.load will see if the dataset exists on the public GCS bucket before building it locally. Returns: ds: `tf.data.Dataset`, the dataset requested, or if `split` is None, a `dict<key: tfds.Split, value: tfds.data.Dataset>`. If `batch_size=-1`, these will be full datasets as `tf.Tensor`s. ds_info: `tfds.core.DatasetInfo`, if `with_info` is True, then `tfds.load` will return a tuple `(ds, ds_info)` containing dataset information (version, features, splits, num_examples,...). Note that the `ds_info` object documents the entire dataset, regardless of the `split` requested. Split-specific information is available in `ds_info.splits`. """ name, name_builder_kwargs = _dataset_name_and_kwargs_from_name_str(name) name_builder_kwargs.update(builder_kwargs or {}) builder_kwargs = name_builder_kwargs # Set data_dir if try_gcs and gcs_utils.is_dataset_on_gcs(name): data_dir = constants.GCS_DATA_DIR elif data_dir is None: data_dir = constants.DATA_DIR dbuilder = builder(name, data_dir=data_dir, **builder_kwargs) if download: download_and_prepare_kwargs = download_and_prepare_kwargs or {} dbuilder.download_and_prepare(**download_and_prepare_kwargs) if as_dataset_kwargs is None: as_dataset_kwargs = {} as_dataset_kwargs = dict(as_dataset_kwargs) as_dataset_kwargs["split"] = split as_dataset_kwargs["as_supervised"] = as_supervised as_dataset_kwargs["batch_size"] = batch_size ds = dbuilder.as_dataset(**as_dataset_kwargs) if with_info: return ds, dbuilder.info return ds
[ "Loads", "the", "named", "dataset", "into", "a", "tf", ".", "data", ".", "Dataset", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/registered.py#L176-L297
[ "def", "load", "(", "name", ",", "split", "=", "None", ",", "data_dir", "=", "None", ",", "batch_size", "=", "1", ",", "download", "=", "True", ",", "as_supervised", "=", "False", ",", "with_info", "=", "False", ",", "builder_kwargs", "=", "None", ",",...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_dataset_name_and_kwargs_from_name_str
Extract kwargs from name str.
tensorflow_datasets/core/registered.py
def _dataset_name_and_kwargs_from_name_str(name_str): """Extract kwargs from name str.""" res = _NAME_REG.match(name_str) if not res: raise ValueError(_NAME_STR_ERR.format(name_str)) name = res.group("dataset_name") kwargs = _kwargs_str_to_kwargs(res.group("kwargs")) try: for attr in ["config", "version"]: val = res.group(attr) if val is None: continue if attr in kwargs: raise ValueError("Dataset %s: cannot pass %s twice." % (name, attr)) kwargs[attr] = val return name, kwargs except: logging.error(_NAME_STR_ERR.format(name_str)) # pylint: disable=logging-format-interpolation raise
def _dataset_name_and_kwargs_from_name_str(name_str): """Extract kwargs from name str.""" res = _NAME_REG.match(name_str) if not res: raise ValueError(_NAME_STR_ERR.format(name_str)) name = res.group("dataset_name") kwargs = _kwargs_str_to_kwargs(res.group("kwargs")) try: for attr in ["config", "version"]: val = res.group(attr) if val is None: continue if attr in kwargs: raise ValueError("Dataset %s: cannot pass %s twice." % (name, attr)) kwargs[attr] = val return name, kwargs except: logging.error(_NAME_STR_ERR.format(name_str)) # pylint: disable=logging-format-interpolation raise
[ "Extract", "kwargs", "from", "name", "str", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/registered.py#L311-L329
[ "def", "_dataset_name_and_kwargs_from_name_str", "(", "name_str", ")", ":", "res", "=", "_NAME_REG", ".", "match", "(", "name_str", ")", "if", "not", "res", ":", "raise", "ValueError", "(", "_NAME_STR_ERR", ".", "format", "(", "name_str", ")", ")", "name", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_cast_to_pod
Try cast to int, float, bool, str, in that order.
tensorflow_datasets/core/registered.py
def _cast_to_pod(val): """Try cast to int, float, bool, str, in that order.""" bools = {"True": True, "False": False} if val in bools: return bools[val] try: return int(val) except ValueError: try: return float(val) except ValueError: return tf.compat.as_text(val)
def _cast_to_pod(val): """Try cast to int, float, bool, str, in that order.""" bools = {"True": True, "False": False} if val in bools: return bools[val] try: return int(val) except ValueError: try: return float(val) except ValueError: return tf.compat.as_text(val)
[ "Try", "cast", "to", "int", "float", "bool", "str", "in", "that", "order", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/registered.py#L343-L354
[ "def", "_cast_to_pod", "(", "val", ")", ":", "bools", "=", "{", "\"True\"", ":", "True", ",", "\"False\"", ":", "False", "}", "if", "val", "in", "bools", ":", "return", "bools", "[", "val", "]", "try", ":", "return", "int", "(", "val", ")", "except...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_try_import
Try importing a module, with an informative error message on failure.
tensorflow_datasets/core/lazy_imports.py
def _try_import(module_name): """Try importing a module, with an informative error message on failure.""" try: mod = importlib.import_module(module_name) return mod except ImportError: err_msg = ("Tried importing %s but failed. See setup.py extras_require. " "The dataset you are trying to use may have additional " "dependencies.") utils.reraise(err_msg)
def _try_import(module_name): """Try importing a module, with an informative error message on failure.""" try: mod = importlib.import_module(module_name) return mod except ImportError: err_msg = ("Tried importing %s but failed. See setup.py extras_require. " "The dataset you are trying to use may have additional " "dependencies.") utils.reraise(err_msg)
[ "Try", "importing", "a", "module", "with", "an", "informative", "error", "message", "on", "failure", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/lazy_imports.py#L27-L36
[ "def", "_try_import", "(", "module_name", ")", ":", "try", ":", "mod", "=", "importlib", ".", "import_module", "(", "module_name", ")", "return", "mod", "except", "ImportError", ":", "err_msg", "=", "(", "\"Tried importing %s but failed. See setup.py extras_require. \...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
np_to_list
Returns list from list, tuple or ndarray.
tensorflow_datasets/core/features/sequence_feature.py
def np_to_list(elem): """Returns list from list, tuple or ndarray.""" if isinstance(elem, list): return elem elif isinstance(elem, tuple): return list(elem) elif isinstance(elem, np.ndarray): return list(elem) else: raise ValueError( 'Input elements of a sequence should be either a numpy array, a ' 'python list or tuple. Got {}'.format(type(elem)))
def np_to_list(elem): """Returns list from list, tuple or ndarray.""" if isinstance(elem, list): return elem elif isinstance(elem, tuple): return list(elem) elif isinstance(elem, np.ndarray): return list(elem) else: raise ValueError( 'Input elements of a sequence should be either a numpy array, a ' 'python list or tuple. Got {}'.format(type(elem)))
[ "Returns", "list", "from", "list", "tuple", "or", "ndarray", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/sequence_feature.py#L257-L268
[ "def", "np_to_list", "(", "elem", ")", ":", "if", "isinstance", "(", "elem", ",", "list", ")", ":", "return", "elem", "elif", "isinstance", "(", "elem", ",", "tuple", ")", ":", "return", "list", "(", "elem", ")", "elif", "isinstance", "(", "elem", ",...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
_transpose_dict_list
Transpose a nested dict[list] into a list[nested dict].
tensorflow_datasets/core/features/sequence_feature.py
def _transpose_dict_list(dict_list): """Transpose a nested dict[list] into a list[nested dict].""" # 1. Unstack numpy arrays into list dict_list = utils.map_nested(np_to_list, dict_list, dict_only=True) # 2. Extract the sequence length (and ensure the length is constant for all # elements) length = {'value': None} # dict because `nonlocal` is Python3 only def update_length(elem): if length['value'] is None: length['value'] = len(elem) elif length['value'] != len(elem): raise ValueError( 'The length of all elements of one sequence should be the same. ' 'Got {} != {}'.format(length['value'], len(elem))) return elem utils.map_nested(update_length, dict_list, dict_only=True) # 3. Extract each individual elements return [ utils.map_nested(lambda elem: elem[i], dict_list, dict_only=True) # pylint: disable=cell-var-from-loop for i in range(length['value']) ]
def _transpose_dict_list(dict_list): """Transpose a nested dict[list] into a list[nested dict].""" # 1. Unstack numpy arrays into list dict_list = utils.map_nested(np_to_list, dict_list, dict_only=True) # 2. Extract the sequence length (and ensure the length is constant for all # elements) length = {'value': None} # dict because `nonlocal` is Python3 only def update_length(elem): if length['value'] is None: length['value'] = len(elem) elif length['value'] != len(elem): raise ValueError( 'The length of all elements of one sequence should be the same. ' 'Got {} != {}'.format(length['value'], len(elem))) return elem utils.map_nested(update_length, dict_list, dict_only=True) # 3. Extract each individual elements return [ utils.map_nested(lambda elem: elem[i], dict_list, dict_only=True) # pylint: disable=cell-var-from-loop for i in range(length['value']) ]
[ "Transpose", "a", "nested", "dict", "[", "list", "]", "into", "a", "list", "[", "nested", "dict", "]", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/sequence_feature.py#L271-L293
[ "def", "_transpose_dict_list", "(", "dict_list", ")", ":", "# 1. Unstack numpy arrays into list", "dict_list", "=", "utils", ".", "map_nested", "(", "np_to_list", ",", "dict_list", ",", "dict_only", "=", "True", ")", "# 2. Extract the sequence length (and ensure the length ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SequenceDict.get_tensor_info
See base class for details.
tensorflow_datasets/core/features/sequence_feature.py
def get_tensor_info(self): """See base class for details.""" # Add the additional length dimension to every shape def add_length_dim(tensor_info): return feature_lib.TensorInfo( shape=(self._length,) + tensor_info.shape, dtype=tensor_info.dtype, ) tensor_info = super(SequenceDict, self).get_tensor_info() return utils.map_nested(add_length_dim, tensor_info)
def get_tensor_info(self): """See base class for details.""" # Add the additional length dimension to every shape def add_length_dim(tensor_info): return feature_lib.TensorInfo( shape=(self._length,) + tensor_info.shape, dtype=tensor_info.dtype, ) tensor_info = super(SequenceDict, self).get_tensor_info() return utils.map_nested(add_length_dim, tensor_info)
[ "See", "base", "class", "for", "details", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/sequence_feature.py#L90-L101
[ "def", "get_tensor_info", "(", "self", ")", ":", "# Add the additional length dimension to every shape", "def", "add_length_dim", "(", "tensor_info", ")", ":", "return", "feature_lib", ".", "TensorInfo", "(", "shape", "=", "(", "self", ".", "_length", ",", ")", "+...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
SequenceDict.get_serialized_info
See base class for details.
tensorflow_datasets/core/features/sequence_feature.py
def get_serialized_info(self): """See base class for details.""" # Add the additional length dimension to every serialized features def add_length_dim(serialized_info): """Add the length dimension to the serialized_info. Args: serialized_info: One of tf.io.FixedLenFeature, tf.io.VarLenFeature,... Returns: new_serialized_info: serialized_info with extended first dimension """ if isinstance(serialized_info, tf.io.FixedLenFeature): if self._length is not None: return tf.io.FixedLenFeature( shape=(self._length,) + serialized_info.shape, dtype=serialized_info.dtype, ) else: return tf.io.FixedLenSequenceFeature( shape=serialized_info.shape, dtype=serialized_info.dtype, allow_missing=True, ) elif isinstance(serialized_info, tf.io.VarLenFeature): return serialized_info else: raise ValueError( 'FixedLenSequenceFeature not supported inside SequenceDict' ) return serialized_info tensor_info = super(SequenceDict, self).get_serialized_info() return utils.map_nested(add_length_dim, tensor_info)
def get_serialized_info(self): """See base class for details.""" # Add the additional length dimension to every serialized features def add_length_dim(serialized_info): """Add the length dimension to the serialized_info. Args: serialized_info: One of tf.io.FixedLenFeature, tf.io.VarLenFeature,... Returns: new_serialized_info: serialized_info with extended first dimension """ if isinstance(serialized_info, tf.io.FixedLenFeature): if self._length is not None: return tf.io.FixedLenFeature( shape=(self._length,) + serialized_info.shape, dtype=serialized_info.dtype, ) else: return tf.io.FixedLenSequenceFeature( shape=serialized_info.shape, dtype=serialized_info.dtype, allow_missing=True, ) elif isinstance(serialized_info, tf.io.VarLenFeature): return serialized_info else: raise ValueError( 'FixedLenSequenceFeature not supported inside SequenceDict' ) return serialized_info tensor_info = super(SequenceDict, self).get_serialized_info() return utils.map_nested(add_length_dim, tensor_info)
[ "See", "base", "class", "for", "details", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/core/features/sequence_feature.py#L103-L137
[ "def", "get_serialized_info", "(", "self", ")", ":", "# Add the additional length dimension to every serialized features", "def", "add_length_dim", "(", "serialized_info", ")", ":", "\"\"\"Add the length dimension to the serialized_info.\n\n Args:\n serialized_info: One of tf.i...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
MNIST._split_generators
Returns SplitGenerators.
tensorflow_datasets/image/mnist.py
def _split_generators(self, dl_manager): """Returns SplitGenerators.""" # Download the full MNIST Database filenames = { "train_data": _MNIST_TRAIN_DATA_FILENAME, "train_labels": _MNIST_TRAIN_LABELS_FILENAME, "test_data": _MNIST_TEST_DATA_FILENAME, "test_labels": _MNIST_TEST_LABELS_FILENAME, } mnist_files = dl_manager.download_and_extract( {k: urllib.parse.urljoin(self.URL, v) for k, v in filenames.items()}) # MNIST provides TRAIN and TEST splits, not a VALIDATION split, so we only # write the TRAIN and TEST splits to disk. return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=10, gen_kwargs=dict( num_examples=_TRAIN_EXAMPLES, data_path=mnist_files["train_data"], label_path=mnist_files["train_labels"], )), tfds.core.SplitGenerator( name=tfds.Split.TEST, num_shards=1, gen_kwargs=dict( num_examples=_TEST_EXAMPLES, data_path=mnist_files["test_data"], label_path=mnist_files["test_labels"], )), ]
def _split_generators(self, dl_manager): """Returns SplitGenerators.""" # Download the full MNIST Database filenames = { "train_data": _MNIST_TRAIN_DATA_FILENAME, "train_labels": _MNIST_TRAIN_LABELS_FILENAME, "test_data": _MNIST_TEST_DATA_FILENAME, "test_labels": _MNIST_TEST_LABELS_FILENAME, } mnist_files = dl_manager.download_and_extract( {k: urllib.parse.urljoin(self.URL, v) for k, v in filenames.items()}) # MNIST provides TRAIN and TEST splits, not a VALIDATION split, so we only # write the TRAIN and TEST splits to disk. return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=10, gen_kwargs=dict( num_examples=_TRAIN_EXAMPLES, data_path=mnist_files["train_data"], label_path=mnist_files["train_labels"], )), tfds.core.SplitGenerator( name=tfds.Split.TEST, num_shards=1, gen_kwargs=dict( num_examples=_TEST_EXAMPLES, data_path=mnist_files["test_data"], label_path=mnist_files["test_labels"], )), ]
[ "Returns", "SplitGenerators", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/mnist.py#L113-L144
[ "def", "_split_generators", "(", "self", ",", "dl_manager", ")", ":", "# Download the full MNIST Database", "filenames", "=", "{", "\"train_data\"", ":", "_MNIST_TRAIN_DATA_FILENAME", ",", "\"train_labels\"", ":", "_MNIST_TRAIN_LABELS_FILENAME", ",", "\"test_data\"", ":", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
MNIST._generate_examples
Generate MNIST examples as dicts. Args: num_examples (int): The number of example. data_path (str): Path to the data files label_path (str): Path to the labels Yields: Generator yielding the next examples
tensorflow_datasets/image/mnist.py
def _generate_examples(self, num_examples, data_path, label_path): """Generate MNIST examples as dicts. Args: num_examples (int): The number of example. data_path (str): Path to the data files label_path (str): Path to the labels Yields: Generator yielding the next examples """ images = _extract_mnist_images(data_path, num_examples) labels = _extract_mnist_labels(label_path, num_examples) data = list(zip(images, labels)) # Data is shuffled automatically to distribute classes uniformly. for image, label in data: yield { "image": image, "label": label, }
def _generate_examples(self, num_examples, data_path, label_path): """Generate MNIST examples as dicts. Args: num_examples (int): The number of example. data_path (str): Path to the data files label_path (str): Path to the labels Yields: Generator yielding the next examples """ images = _extract_mnist_images(data_path, num_examples) labels = _extract_mnist_labels(label_path, num_examples) data = list(zip(images, labels)) # Data is shuffled automatically to distribute classes uniformly. for image, label in data: yield { "image": image, "label": label, }
[ "Generate", "MNIST", "examples", "as", "dicts", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/mnist.py#L146-L166
[ "def", "_generate_examples", "(", "self", ",", "num_examples", ",", "data_path", ",", "label_path", ")", ":", "images", "=", "_extract_mnist_images", "(", "data_path", ",", "num_examples", ")", "labels", "=", "_extract_mnist_labels", "(", "label_path", ",", "num_e...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
OxfordFlowers102._split_generators
Returns SplitGenerators.
tensorflow_datasets/image/oxford_flowers102.py
def _split_generators(self, dl_manager): """Returns SplitGenerators.""" # Download images and annotations that come in separate archives. # Note, that the extension of archives is .tar.gz even though the actual # archives format is uncompressed tar. dl_paths = dl_manager.download_and_extract({ "images": tfds.download.Resource( url=os.path.join(_BASE_URL, "102flowers.tgz"), extract_method=tfds.download.ExtractMethod.TAR), "labels": os.path.join(_BASE_URL, "imagelabels.mat"), "setid": os.path.join(_BASE_URL, "setid.mat"), }) gen_kwargs = dict( images_dir_path=os.path.join(dl_paths["images"], "jpg"), labels_path=dl_paths["labels"], setid_path=dl_paths["setid"], ) return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=1, gen_kwargs=dict(split_name="trnid", **gen_kwargs)), tfds.core.SplitGenerator( name=tfds.Split.TEST, num_shards=1, gen_kwargs=dict(split_name="tstid", **gen_kwargs)), tfds.core.SplitGenerator( name=tfds.Split.VALIDATION, num_shards=1, gen_kwargs=dict(split_name="valid", **gen_kwargs)), ]
def _split_generators(self, dl_manager): """Returns SplitGenerators.""" # Download images and annotations that come in separate archives. # Note, that the extension of archives is .tar.gz even though the actual # archives format is uncompressed tar. dl_paths = dl_manager.download_and_extract({ "images": tfds.download.Resource( url=os.path.join(_BASE_URL, "102flowers.tgz"), extract_method=tfds.download.ExtractMethod.TAR), "labels": os.path.join(_BASE_URL, "imagelabels.mat"), "setid": os.path.join(_BASE_URL, "setid.mat"), }) gen_kwargs = dict( images_dir_path=os.path.join(dl_paths["images"], "jpg"), labels_path=dl_paths["labels"], setid_path=dl_paths["setid"], ) return [ tfds.core.SplitGenerator( name=tfds.Split.TRAIN, num_shards=1, gen_kwargs=dict(split_name="trnid", **gen_kwargs)), tfds.core.SplitGenerator( name=tfds.Split.TEST, num_shards=1, gen_kwargs=dict(split_name="tstid", **gen_kwargs)), tfds.core.SplitGenerator( name=tfds.Split.VALIDATION, num_shards=1, gen_kwargs=dict(split_name="valid", **gen_kwargs)), ]
[ "Returns", "SplitGenerators", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/oxford_flowers102.py#L70-L102
[ "def", "_split_generators", "(", "self", ",", "dl_manager", ")", ":", "# Download images and annotations that come in separate archives.", "# Note, that the extension of archives is .tar.gz even though the actual", "# archives format is uncompressed tar.", "dl_paths", "=", "dl_manager", ...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc
train
OxfordFlowers102._generate_examples
Yields examples.
tensorflow_datasets/image/oxford_flowers102.py
def _generate_examples(self, images_dir_path, labels_path, setid_path, split_name): """Yields examples.""" with tf.io.gfile.GFile(labels_path, "rb") as f: labels = tfds.core.lazy_imports.scipy.io.loadmat(f)["labels"][0] with tf.io.gfile.GFile(setid_path, "rb") as f: examples = tfds.core.lazy_imports.scipy.io.loadmat(f)[split_name][0] for image_id in examples: file_name = "image_%05d.jpg" % image_id yield { "image": os.path.join(images_dir_path, file_name), "label": labels[image_id - 1] - 1, "file_name": file_name, }
def _generate_examples(self, images_dir_path, labels_path, setid_path, split_name): """Yields examples.""" with tf.io.gfile.GFile(labels_path, "rb") as f: labels = tfds.core.lazy_imports.scipy.io.loadmat(f)["labels"][0] with tf.io.gfile.GFile(setid_path, "rb") as f: examples = tfds.core.lazy_imports.scipy.io.loadmat(f)[split_name][0] for image_id in examples: file_name = "image_%05d.jpg" % image_id yield { "image": os.path.join(images_dir_path, file_name), "label": labels[image_id - 1] - 1, "file_name": file_name, }
[ "Yields", "examples", "." ]
tensorflow/datasets
python
https://github.com/tensorflow/datasets/blob/46ceb0cf7b4690f38ecbbc689e4d659a903d08dc/tensorflow_datasets/image/oxford_flowers102.py#L104-L118
[ "def", "_generate_examples", "(", "self", ",", "images_dir_path", ",", "labels_path", ",", "setid_path", ",", "split_name", ")", ":", "with", "tf", ".", "io", ".", "gfile", ".", "GFile", "(", "labels_path", ",", "\"rb\"", ")", "as", "f", ":", "labels", "...
46ceb0cf7b4690f38ecbbc689e4d659a903d08dc