_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q8500 | RequestHandler.get | train | def get(self, url, params=None):
"""
Initiate a GET request
"""
r = self.session.get(url, params=params) | python | {
"resource": ""
} |
q8501 | RequestHandler.post | train | def post(self, url, data, params=None):
"""
Initiate a POST request
"""
r = self.session.post(url, data=data, | python | {
"resource": ""
} |
q8502 | RequestHandler.send | train | def send(self, request, expect_json=True, ignore_content=False):
"""
Send a formatted API request
:param request: a formatted request object
:type request: :class:`.Request`
:param bool expect_json: if True, raise :class:`.InvalidAPIAccess` if
response is not in JSON format
:param bool ignore_content: whether to ignore setting content of the
Response object
"""
r = self.session.request(method=request.method,
| python | {
"resource": ""
} |
q8503 | API.login | train | def login(self, username=None, password=None,
section='default'):
"""
Created the passport with ``username`` and ``password`` and log in.
If either ``username`` or ``password`` is None or omitted, the
credentials file will be parsed.
:param str username: username to login (email, phone number or user ID)
:param str password: password
:param str section: section name in the credential file
:raise: raises :class:`.AuthenticationError` if failed to login
"""
if self.has_logged_in:
return True
if username is None or password is None:
credential = conf.get_credential(section)
username = credential['username']
password = credential['password']
passport = Passport(username, password)
r = self.http.post(LOGIN_URL, passport.form)
if r.state is True:
# Bind this passport to API
self.passport = passport
| python | {
"resource": ""
} |
q8504 | API.user_id | train | def user_id(self):
"""
User id of the current API user
"""
if self._user_id is None:
if self.has_logged_in:
self._user_id | python | {
"resource": ""
} |
q8505 | API.username | train | def username(self):
"""
Username of the current API user
"""
if self._username is None:
if self.has_logged_in:
self._username = self._get_username()
| python | {
"resource": ""
} |
q8506 | API.has_logged_in | train | def has_logged_in(self):
"""Check whether the API has logged in"""
r = self.http.get(CHECKPOINT_URL)
| python | {
"resource": ""
} |
q8507 | API.receiver_directory | train | def receiver_directory(self):
"""Parent directory of the downloads directory"""
if self._receiver_directory | python | {
"resource": ""
} |
q8508 | API.add_task_bt | train | def add_task_bt(self, filename, select=False):
"""
Add a new BT task
:param str filename: path to torrent file to upload
:param bool select: whether to select files in the torrent.
* True: it returns the opened torrent (:class:`.Torrent`) and
| python | {
"resource": ""
} |
q8509 | API.get_storage_info | train | def get_storage_info(self, human=False):
"""
Get storage info
:param bool human: whether return human-readable size
:return: total and used storage
:rtype: dict
"""
res = self._req_get_storage_info()
| python | {
"resource": ""
} |
q8510 | API.upload | train | def upload(self, filename, directory=None):
"""
Upload a file ``filename`` to ``directory``
:param str filename: path to the file to upload
:param directory: destionation :class:`.Directory`, defaults to
:attribute:`.API.downloads_directory` if None
:return: the uploaded file
:rtype: :class:`.File`
"""
filename = eval_path(filename)
if directory is None:
directory = self.downloads_directory
# First request
| python | {
"resource": ""
} |
q8511 | API.download | train | def download(self, obj, path=None, show_progress=True, resume=True,
auto_retry=True, proapi=False):
"""
Download a file
:param obj: :class:`.File` object
:param str path: local path
:param bool show_progress: whether to show download progress
:param bool resume: whether to resume on | python | {
"resource": ""
} |
q8512 | API.search | train | def search(self, keyword, count=30):
"""
Search files or directories
:param str keyword: keyword
:param int count: number of entries to be listed
"""
kwargs = {}
kwargs['search_value'] = keyword
root = self.root_directory
entries = root._load_entries(func=self._req_files_search,
count=count, page=1, **kwargs)
res = []
| python | {
"resource": ""
} |
q8513 | API._req_offline_space | train | def _req_offline_space(self):
"""Required before accessing lixian tasks"""
url = 'http://115.com/'
params = {
'ct': 'offline',
'ac': 'space',
'_': get_timestamp(13)
}
_sign = os.environ.get('U115_BROWSER_SIGN')
if _sign is not None:
_time = os.environ.get('U115_BROWSER_TIME')
if _time is None:
msg = 'U115_BROWSER_TIME is required given U115_BROWSER_SIGN.'
raise APIError(msg)
params['sign'] = _sign
| python | {
"resource": ""
} |
q8514 | API._req_lixian_task_lists | train | def _req_lixian_task_lists(self, page=1):
"""
This request will cause the system to create a default downloads
directory if it does not exist
"""
url = 'http://115.com/lixian/'
params = {'ct': 'lixian', 'ac': 'task_lists'}
self._load_signatures()
data = {
'page': page,
'uid': self.user_id,
'sign': self._signatures['offline_space'],
'time': self._lixian_timestamp,
}
req = Request(method='POST', url=url, params=params, data=data)
| python | {
"resource": ""
} |
q8515 | API._req_lixian_get_id | train | def _req_lixian_get_id(self, torrent=False):
"""Get `cid` of lixian space directory"""
url = 'http://115.com/'
params = {
'ct': 'lixian',
'ac': 'get_id',
'torrent': 1 if torrent else None,
| python | {
"resource": ""
} |
q8516 | API._req_files_edit | train | def _req_files_edit(self, fid, file_name=None, is_mark=0):
"""Edit a file or directory"""
url = self.web_api_url + '/edit'
data = locals()
del data['self']
req = Request(method='POST', url=url, data=data)
res = self.http.send(req)
| python | {
"resource": ""
} |
q8517 | API._req_directory | train | def _req_directory(self, cid):
"""Return name and pid of by cid"""
res = self._req_files(cid=cid, offset=0, limit=1, show_dir=1)
path = res['path']
count = res['count']
for d in path:
if str(d['cid']) == str(cid):
res = {
'cid': d['cid'],
| python | {
"resource": ""
} |
q8518 | API._req_upload | train | def _req_upload(self, filename, directory):
"""Raw request to upload a file ``filename``"""
self._upload_url = self._load_upload_url()
self.http.get('http://upload.115.com/crossdomain.xml')
b = os.path.basename(filename)
target = 'U_1_' + str(directory.cid)
files = {
'Filename': ('', quote(b), ''),
'target': ('', target, ''),
'Filedata': (quote(b), open(filename, 'rb'), ''),
'Upload': ('', 'Submit Query', ''),
}
req = Request(method='POST', url=self._upload_url, files=files)
res = self.http.send(req)
| python | {
"resource": ""
} |
q8519 | API._load_root_directory | train | def _load_root_directory(self):
"""
Load root directory, which has a cid of 0
"""
| python | {
"resource": ""
} |
q8520 | API._load_torrents_directory | train | def _load_torrents_directory(self):
"""
Load torrents directory
If it does not exist yet, this request will cause | python | {
"resource": ""
} |
q8521 | API._load_downloads_directory | train | def _load_downloads_directory(self):
"""
Load downloads directory
If it does not exist yet, this request will | python | {
"resource": ""
} |
q8522 | API._parse_src_js_var | train | def _parse_src_js_var(self, variable):
"""Parse JavaScript variables in the source page"""
src_url = 'http://115.com'
r = self.http.get(src_url)
soup = BeautifulSoup(r.content)
scripts = [script.text for script in soup.find_all('script')]
text = '\n'.join(scripts)
pattern = "%s\s*=\s*(.*);" % (variable.upper())
| python | {
"resource": ""
} |
q8523 | BaseFile.delete | train | def delete(self):
"""
Delete this file or directory
:return: whether deletion is successful
:raise: :class:`.APIError` if this file or directory is already deleted
"""
fcid = None
pid = None
if isinstance(self, File):
fcid = self.fid
pid = self.cid
elif isinstance(self, Directory):
fcid = self.cid
pid = self.pid
else:
| python | {
"resource": ""
} |
q8524 | BaseFile.edit | train | def edit(self, name, mark=False):
"""
Edit this file or directory
:param str name: new name for this entry
:param bool mark: | python | {
"resource": ""
} |
q8525 | File.directory | train | def directory(self):
"""Directory that holds this file"""
if self._directory is | python | {
"resource": ""
} |
q8526 | File.get_download_url | train | def get_download_url(self, proapi=False):
"""
Get this file's download URL
:param bool proapi: whether to use pro API
"""
if self._download_url is None:
| python | {
"resource": ""
} |
q8527 | File.download | train | def download(self, path=None, show_progress=True, resume=True,
auto_retry=True, proapi=False):
"""Download this file"""
| python | {
"resource": ""
} |
q8528 | File.reload | train | def reload(self):
"""
Reload file info and metadata
* name
* sha
* pickcode
"""
| python | {
"resource": ""
} |
q8529 | Directory.parent | train | def parent(self):
"""Parent directory that holds this directory"""
if self._parent is None: | python | {
"resource": ""
} |
q8530 | Directory.reload | train | def reload(self):
"""
Reload directory info and metadata
* `name`
* `pid`
* `count`
"""
r = self.api._req_directory(self.cid)
| python | {
"resource": ""
} |
q8531 | Directory.list | train | def list(self, count=30, order='user_ptime', asc=False, show_dir=True,
natsort=True):
"""
List directory contents
:param int count: number of entries to be listed
:param str order: order of entries, originally named `o`. This value
may be one of `user_ptime` (default), `file_size` and `file_name`
:param bool asc: whether in ascending order
:param bool show_dir: whether to show directories
:param bool natsort: whether to use natural sort
Return a list of :class:`.File` or :class:`.Directory` objects
"""
if self.cid is None:
return False
self.reload()
kwargs = {}
# `cid` is the only required argument
kwargs['cid'] = self.cid
kwargs['asc'] = 1 if asc is True else 0
kwargs['show_dir'] = 1 if show_dir is True else 0
kwargs['natsort'] = 1 if natsort is True else 0
kwargs['o'] = order
# When the downloads directory exists along with its parent directory,
# the receiver directory, its parent's count (receiver directory's
# count) does not include the downloads directory. This behavior is
# similar to its parent's parent (root), the count of which does not
# include the receiver directory.
# The following code fixed this behavior so that a directory's
# count correctly reflects the actual number of entries in it
# The side-effect that this code may ensure that downloads directory
# exists, causing the system to create the receiver directory and
# downloads | python | {
"resource": ""
} |
q8532 | Task.status_human | train | def status_human(self):
"""
Human readable status
:return:
* `DOWNLOADING`: the task is downloading files
* `BEING TRANSFERRED`: the task is being transferred
* `TRANSFERRED`: the task has been transferred to downloads \
directory
* `SEARCHING RESOURCES`: the task is searching resources
* `FAILED`: the task is failed
* `DELETED`: the task is deleted
* `UNKNOWN STATUS`
:rtype: str
"""
res = None
if self._deleted:
return 'DELETED'
if self.status == 1:
res = 'DOWNLOADING'
elif self.status == 2:
if self.move == 0:
res | python | {
"resource": ""
} |
q8533 | Task.directory | train | def directory(self):
"""Associated directory, if any, with this task"""
if not self.is_directory:
msg = 'This task is a file task with no associated directory.'
raise TaskError(msg)
if self._directory is None:
if self.is_transferred:
self._directory = self.api._load_directory(self.cid) | python | {
"resource": ""
} |
q8534 | Task.list | train | def list(self, count=30, order='user_ptime', asc=False, show_dir=True,
natsort=True):
"""
List files of the associated directory to this task.
:param int count: number of entries to be listed
:param str order: originally named `o`
:param bool asc: whether in ascending order | python | {
"resource": ""
} |
q8535 | Torrent.submit | train | def submit(self):
"""Submit this torrent and create a new task"""
if self.api._req_lixian_add_task_bt(self):
| python | {
"resource": ""
} |
q8536 | Command.get_needful_files | train | def get_needful_files(self):
"""
Returns currently used static files.
Assumes that manifest staticfiles.json is up-to-date.
"""
manifest = self.storage.load_manifest()
if self.keep_unhashed_files:
if PY3:
| python | {
"resource": ""
} |
q8537 | Command.model_file_fields | train | def model_file_fields(self, model):
"""
Generator yielding all instances of FileField and its subclasses of a model.
"""
| python | {
"resource": ""
} |
q8538 | Command.get_resource_types | train | def get_resource_types(self):
"""
Returns set of resource types of FileFields of all registered models.
Needed by Cloudinary as resource type is needed to browse or delete specific files.
"""
resource_types = set()
for model in self.models():
| python | {
"resource": ""
} |
q8539 | Command.get_needful_files | train | def get_needful_files(self):
"""
Returns set of media files associated with models.
Those files won't be deleted.
"""
needful_files = []
for model in self.models():
media_fields = []
for field in self.model_file_fields(model):
| python | {
"resource": ""
} |
q8540 | Command.get_files_to_remove | train | def get_files_to_remove(self):
"""
Returns orphaned media files to be removed grouped by resource type.
All files which paths start with any of exclude paths are ignored.
"""
files_to_remove = {}
needful_files = self.get_needful_files()
for resources_type, resources in self.get_uploaded_resources():
exclude_paths = self.get_exclude_paths()
| python | {
"resource": ""
} |
q8541 | StaticCloudinaryStorage._get_resource_type | train | def _get_resource_type(self, name):
"""
Implemented as static files can be of different resource types.
Because web developers are the people who control those files, we can distinguish them
simply by looking at their extensions, we don't need any content based validation.
"""
extension = self._get_file_extension(name)
if extension is None:
| python | {
"resource": ""
} |
q8542 | StaticCloudinaryStorage._remove_extension_for_non_raw_file | train | def _remove_extension_for_non_raw_file(self, name):
"""
Implemented as image and video files' Cloudinary public id
shouldn't contain file extensions, otherwise Cloudinary url
would contain doubled extension - Cloudinary adds extension to url
to allow file conversion to arbitrary file, like png to jpg.
"""
| python | {
"resource": ""
} |
q8543 | StaticCloudinaryStorage._exists_with_etag | train | def _exists_with_etag(self, name, content):
"""
Checks whether a file with a name and a content is already uploaded to Cloudinary.
Uses ETAG header and MD5 hash for the content comparison.
"""
url = self._get_url(name)
response = requests.head(url)
| python | {
"resource": ""
} |
q8544 | StaticCloudinaryStorage._save | train | def _save(self, name, content):
"""
Saves only when a file with a name and a content is not already uploaded to Cloudinary.
"""
name = self.clean_name(name) # | python | {
"resource": ""
} |
q8545 | BaseCart.add | train | def add(self, pk, quantity=1, **kwargs):
"""Add an item to the cart.
If the item is already in the cart, then its quantity will be
increased by `quantity` units.
Parameters
----------
pk : str or int
The primary key of the item.
quantity : int-convertible
A number of units of to add.
**kwargs
Extra keyword arguments to pass to the item class
constructor.
| python | {
"resource": ""
} |
q8546 | BaseCart.change_quantity | train | def change_quantity(self, pk, quantity):
"""Change the quantity of an item.
Parameters
----------
pk : str or int
The primary key of the item.
quantity : int-convertible
A new quantity.
Raises
------
ItemNotInCart
NegativeItemQuantity
NonConvertibleItemQuantity
TooLargeItemQuantity
ZeroItemQuantity
| python | {
"resource": ""
} |
q8547 | BaseCart.remove | train | def remove(self, pk):
"""Remove an item from the cart.
Parameters
----------
pk : str or int
The primary key of the item.
| python | {
"resource": ""
} |
q8548 | BaseCart.list_items | train | def list_items(self, sort_key=None, reverse=False):
"""Return a list of cart items.
Parameters
----------
sort_key : func
A function to customize the list order, same as the 'key'
argument to the built-in :func:`sorted`.
reverse: bool
If set to True, the sort order will be reversed.
Returns
-------
list
List of :attr:`item_class` instances.
Examples
--------
>>> cart = Cart(request)
>>> cart.list_items(lambda item: item.obj.name)
[<CartItem: obj=bar, quantity=3>,
<CartItem: obj=foo, quantity=1>,
| python | {
"resource": ""
} |
q8549 | BaseCart.encode | train | def encode(self, formatter=None):
"""Return a representation of the cart as a JSON-response.
Parameters
----------
formatter : func, optional
A function that accepts the cart representation and returns
its formatted version.
Returns
-------
django.http.JsonResponse
Examples
--------
Assume that items with primary keys "1" and "4" are already in
the cart.
>>> cart = Cart(request)
>>> def format_total_price(cart_repr):
... return intcomma(cart_repr['totalPrice'])
...
>>> json_response = cart.encode(format_total_price)
>>> json_response.content
b'{
"items": {
'1': {"price": 100, "quantity": 10, "total": 1000},
'4': {"price": 50, "quantity": 20, "total": 1000},
},
"itemCount": 2,
"totalPrice": "2,000",
}'
"""
| python | {
"resource": ""
} |
q8550 | BaseCart.create_items | train | def create_items(self, session_items):
"""Instantiate cart items from session data.
The value returned by this method is used to populate the
cart's `items` attribute.
Parameters
----------
session_items : dict
A dictionary of pk-quantity mappings (each pk is a string).
For example: ``{'1': 5, '3': 2}``.
Returns
-------
dict
A map between the `session_items` keys and instances of
:attr:`item_class`. For example::
{'1': <CartItem: obj=foo, quantity=5>,
'3': <CartItem: obj=bar, quantity=2>}
| python | {
"resource": ""
} |
q8551 | BaseCart.update | train | def update(self):
"""Update the cart.
First this method updates attributes dependent on the cart's
`items`, such as `total_price` or `item_count`.
After that, it saves the new cart state to the session.
Generally, you'll need to call this method by yourself, only
when implementing new methods that directly change the `items`
attribute.
"""
self.item_count = self.count_items()
self.total_price = self.count_total_price()
# Update the session
session = self.request.session
session_items = {}
for pk, item in self.items.items():
| python | {
"resource": ""
} |
q8552 | BaseCart.count_items | train | def count_items(self, unique=True):
"""Count items in the cart.
Parameters
----------
unique : bool-convertible, optional
Returns
-------
int
If `unique` is truthy, then the result is the number of
items | python | {
"resource": ""
} |
q8553 | hill_climber | train | def hill_climber(objective_function,
initial_array,
lower_bound=-float('inf'),
acceptance_criteria=None,
max_iterations=10 ** 3):
"""
Implement a basic hill climbing algorithm.
Has two stopping conditions:
1. Maximum number of iterations;
2. A known lower bound, a none is passed then this is not used.
If acceptance_criteria (a callable) is not None then this is used to obtain
an upper bound on some other measure (different to the objective function).
In practice this is used when optimising the objective function to ensure
that we don't accept a solution that improves the objective function but tht
adds more constraint violations.
"""
X = initial_array
if acceptance_criteria is not None:
acceptance_bound = acceptance_criteria(X)
iterations = 0
current_energy = | python | {
"resource": ""
} |
q8554 | simulated_annealing | train | def simulated_annealing(objective_function,
initial_array,
initial_temperature=10 ** 4,
cooldown_rate=0.7,
acceptance_criteria=None,
lower_bound=-float('inf'),
max_iterations=10 ** 3):
"""
Implement a simulated annealing algorithm with exponential cooling
Has two stopping conditions:
1. Maximum number of iterations;
2. A known lower bound, a none is passed then this is not used.
Note that starting with an initial_temperature corresponds to a hill
climbing algorithm
"""
X = initial_array
if acceptance_criteria is not None:
acceptance_bound = acceptance_criteria(X)
best_X = | python | {
"resource": ""
} |
q8555 | _events_available_in_scheduled_slot | train | def _events_available_in_scheduled_slot(events, slots, X, **kwargs):
"""
Constraint that ensures that an event is scheduled in slots for which it is
available
"""
slot_availability_array = lpu.slot_availability_array(slots=slots,
| python | {
"resource": ""
} |
q8556 | _events_available_during_other_events | train | def _events_available_during_other_events(
events, slots, X, summation_type=None, **kwargs
):
"""
Constraint that ensures that an event is not scheduled at the same time as
another event for which it is unavailable. Unavailability of events is
either because it is explicitly defined or because they share a tag.
"""
summation = lpu.summation_functions[summation_type]
event_availability_array = lpu.event_availability_array(events)
label = 'Event clashes with another event'
for slot1, slot2 in lpu.concurrent_slots(slots):
for row, event in enumerate(event_availability_array):
if events[row].unavailability:
for col, availability in enumerate(event):
| python | {
"resource": ""
} |
q8557 | _upper_bound_on_event_overflow | train | def _upper_bound_on_event_overflow(
events, slots, X, beta, summation_type=None, **kwargs
):
"""
This is an artificial constraint that is used by the objective function
aiming to minimise the maximum overflow in a slot.
"""
label = 'Artificial upper bound constraint'
for row, event in enumerate(events):
| python | {
"resource": ""
} |
q8558 | heuristic | train | def heuristic(events,
slots,
objective_function=None,
algorithm=heu.hill_climber,
initial_solution=None,
initial_solution_algorithm_kwargs={},
objective_function_algorithm_kwargs={},
**kwargs):
"""
Compute a schedule using a heuristic
Parameters
----------
events : list or tuple
of :py:class:`resources.Event` instances
slots : list or tuple
of :py:class:`resources.Slot` instances
algorithm : callable
a heuristic algorithm from conference_scheduler.heuristics
initial_solution_algorithm_kwargs : dict
kwargs for the heuristic algorithm for the initial solution
objective_function_algorithm_kwargs : dict
kwargs for the heuristic algorithm for the objective function (if
necessary.
objective_function: callable
from lp_problem.objective_functions
kwargs : keyword arguments
arguments for the objective function
Returns
-------
list
A list of tuples giving the event and slot index (for the given
events and slots lists) for all scheduled items.
Example
-------
For a solution where
* event 0 is scheduled in slot 1
* event 1 is scheduled in slot 4
* event 2 is scheduled in slot 5
| python | {
"resource": ""
} |
q8559 | solution | train | def solution(events, slots, objective_function=None, solver=None, **kwargs):
"""Compute a schedule in solution form
Parameters
----------
events : list or tuple
of :py:class:`resources.Event` instances
slots : list or tuple
of :py:class:`resources.Slot` instances
solver : pulp.solver
a pulp solver
objective_function: callable
from lp_problem.objective_functions
kwargs : keyword arguments
arguments for the objective function
Returns
-------
list
A list of tuples giving the event and slot index (for the given
events and slots lists) for all scheduled items.
Example
-------
For a solution where
* event 0 is scheduled in slot 1
* event 1 is scheduled in slot 4
* event 2 is scheduled in slot 5
the resulting list would be::
[(0, 1), (1, 4), (2, 5)]
"""
shape = Shape(len(events), len(slots))
problem = pulp.LpProblem()
X = lp.utils.variables(shape)
beta = pulp.LpVariable("upper_bound")
for constraint in lp.constraints.all_constraints(
| python | {
"resource": ""
} |
q8560 | array | train | def array(events, slots, objective_function=None, solver=None, **kwargs):
"""Compute a schedule in array form
Parameters
----------
events : list or tuple
of :py:class:`resources.Event` instances
slots : list or tuple
of :py:class:`resources.Slot` instances
objective_function : callable
from lp_problem.objective_functions
Returns
-------
np.array
An E by S array (X) where E is the number of events and S the
number of slots. Xij is 1 if event i is scheduled in slot j and
zero otherwise
Example
-------
For 3 events, 7 slots and a solution where
| python | {
"resource": ""
} |
q8561 | schedule | train | def schedule(events, slots, objective_function=None, solver=None, **kwargs):
"""Compute a schedule in schedule form
Parameters
----------
events : list or tuple
of :py:class:`resources.Event` instances
slots : list or tuple
of :py:class:`resources.Slot` instances
solver | python | {
"resource": ""
} |
q8562 | event_schedule_difference | train | def event_schedule_difference(old_schedule, new_schedule):
"""Compute the difference between two schedules from an event perspective
Parameters
----------
old_schedule : list or tuple
of :py:class:`resources.ScheduledItem` objects
new_schedule : list or tuple
of :py:class:`resources.ScheduledItem` objects
Returns
-------
list
A list of :py:class:`resources.ChangedEventScheduledItem` objects
Example
-------
>>> from conference_scheduler.resources import Event, Slot, ScheduledItem
>>> from conference_scheduler.scheduler import event_schedule_difference
>>> events = [Event(f'event_{i}', 30, 0) for i in range(5)]
>>> slots = [Slot(f'venue_{i}', '', 30, 100, None) for i in range(5)]
>>> old_schedule = (
... ScheduledItem(events[0], slots[0]),
... ScheduledItem(events[1], slots[1]),
| python | {
"resource": ""
} |
q8563 | slot_schedule_difference | train | def slot_schedule_difference(old_schedule, new_schedule):
"""Compute the difference between two schedules from a slot perspective
Parameters
----------
old_schedule : list or tuple
of :py:class:`resources.ScheduledItem` objects
new_schedule : list or tuple
of :py:class:`resources.ScheduledItem` objects
Returns
-------
list
A list of :py:class:`resources.ChangedSlotScheduledItem` objects
Example
-------
>>> from conference_scheduler.resources import Event, Slot, ScheduledItem
>>> from conference_scheduler.scheduler import slot_schedule_difference
>>> events = [Event(f'event_{i}', 30, 0) for i in range(5)]
>>> slots = [Slot(f'venue_{i}', '', 30, 100, None) for i in range(5)]
>>> old_schedule = (
... ScheduledItem(events[0], slots[0]),
... ScheduledItem(events[1], slots[1]),
... | python | {
"resource": ""
} |
q8564 | array_violations | train | def array_violations(array, events, slots, beta=None):
"""Take a schedule in array form and return any violated constraints
Parameters
----------
array : np.array
a schedule in array form
events : list or tuple
of resources.Event instances
slots : list or tuple
of resources.Slot instances
constraints : list or tuple
of generator functions which each produce instances of
resources.Constraint
Returns
| python | {
"resource": ""
} |
q8565 | is_valid_array | train | def is_valid_array(array, events, slots):
"""Take a schedule in array form and return whether it is a valid
solution for the given constraints
Parameters
----------
array : np.array
a schedule in array form
events : list or tuple
of resources.Event instances
slots : list or tuple
of resources.Slot instances
Returns
-------
bool
| python | {
"resource": ""
} |
q8566 | is_valid_solution | train | def is_valid_solution(solution, events, slots):
"""Take a solution and return whether it is valid for the
given constraints
Parameters
----------
solution: list or tuple
a schedule in solution form
events : list or tuple
of resources.Event instances
slots : list or tuple
of resources.Slot instances
Returns
-------
bool
| python | {
"resource": ""
} |
q8567 | solution_violations | train | def solution_violations(solution, events, slots):
"""Take a solution and return a list of violated constraints
Parameters
----------
solution: list or tuple
a schedule in solution form
events : list or tuple
of resources.Event instances
slots : list or tuple
of resources.Slot instances
Returns
-------
Generator
| python | {
"resource": ""
} |
q8568 | is_valid_schedule | train | def is_valid_schedule(schedule, events, slots):
"""Take a schedule and return whether it is a valid solution for the
given constraints
Parameters
----------
schedule : list or tuple
a schedule in schedule form
events : list or tuple
of resources.Event instances
slots : list or tuple
of resources.Slot instances
Returns
-------
bool
| python | {
"resource": ""
} |
q8569 | schedule_violations | train | def schedule_violations(schedule, events, slots):
"""Take a schedule and return a list of violated constraints
Parameters
----------
schedule : list or tuple
a schedule in schedule form
events : list or tuple
of resources.Event instances
slots : list or tuple
of resources.Slot instances
Returns
-------
Generator
| python | {
"resource": ""
} |
q8570 | tag_array | train | def tag_array(events):
"""
Return a numpy array mapping events to tags
- Rows corresponds to events
- Columns correspond to tags
"""
all_tags = sorted(set(tag for event in events for tag in event.tags))
array = np.zeros((len(events), len(all_tags))) | python | {
"resource": ""
} |
q8571 | session_array | train | def session_array(slots):
"""
Return a numpy array mapping sessions to slots
- Rows corresponds to sessions
- Columns correspond to slots
"""
# Flatten the list: this assumes that the sessions do not share slots
sessions = sorted(set([slot.session for slot | python | {
"resource": ""
} |
q8572 | slot_availability_array | train | def slot_availability_array(events, slots):
"""
Return a numpy array mapping events to slots
- Rows corresponds to events
- Columns correspond to stags
Array has value 0 if event cannot be scheduled in a given slot
(1 otherwise)
"""
array = np.ones((len(events), len(slots)))
for row, event in enumerate(events):
| python | {
"resource": ""
} |
q8573 | event_availability_array | train | def event_availability_array(events):
"""
Return a numpy array mapping events to events
- Rows corresponds to events
- Columns correspond to events
Array has value 0 if event cannot be scheduled at same time as other event
(1 otherwise)
"""
array = np.ones((len(events), len(events)))
for row, event in enumerate(events):
for col, other_event in enumerate(events):
if row != col:
tags = set(event.tags)
| python | {
"resource": ""
} |
q8574 | concurrent_slots | train | def concurrent_slots(slots):
"""
Yields all concurrent slot indices.
"""
for i, slot in enumerate(slots):
for j, other_slot in enumerate(slots[i + 1:]):
| python | {
"resource": ""
} |
q8575 | _events_with_diff_tag | train | def _events_with_diff_tag(talk, tag_array):
"""
Return the indices of the events with no tag in common as tag
"""
| python | {
"resource": ""
} |
q8576 | solution_to_array | train | def solution_to_array(solution, events, slots):
"""Convert a schedule from solution to array form
Parameters
----------
solution : list or tuple
of tuples of event index and slot index for each scheduled item
events : list or tuple
of :py:class:`resources.Event` instances
slots : list or tuple
of :py:class:`resources.Slot` instances
Returns
-------
np.array
An E by S array (X) where E is the number of events and S the
number of slots. Xij is 1 if event i is scheduled in slot j and
zero otherwise
Example | python | {
"resource": ""
} |
q8577 | solution_to_schedule | train | def solution_to_schedule(solution, events, slots):
"""Convert a schedule from solution to schedule form
Parameters
----------
solution : list or tuple
of tuples of event index and slot index for each scheduled item
events : list or tuple
of :py:class:`resources.Event` instances
slots : list or tuple
of :py:class:`resources.Slot` instances
| python | {
"resource": ""
} |
q8578 | schedule_to_array | train | def schedule_to_array(schedule, events, slots):
"""Convert a schedule from schedule to array form
Parameters
----------
schedule : list or tuple
of instances of :py:class:`resources.ScheduledItem`
events : list or tuple
of :py:class:`resources.Event` instances
slots : list or tuple
of :py:class:`resources.Slot` instances
Returns
-------
np.array
An E by S array (X) where E is the number of events and S the
number of slots. Xij is 1 if event i is | python | {
"resource": ""
} |
q8579 | array_to_schedule | train | def array_to_schedule(array, events, slots):
"""Convert a schedule from array to schedule form
Parameters
----------
array : np.array
An E by S array (X) where E is the number of events and S the
number of slots. Xij is 1 if event i is scheduled in slot j and
zero otherwise
events : list or tuple
of :py:class:`resources.Event` instances
slots : list or tuple
of :py:class:`resources.Slot` instances
Returns
-------
list
A | python | {
"resource": ""
} |
q8580 | add_line | train | def add_line(self, line, source, *lineno):
"""Append one line of generated reST to the output."""
if 'conference_scheduler.scheduler' in source: | python | {
"resource": ""
} |
q8581 | efficiency_capacity_demand_difference | train | def efficiency_capacity_demand_difference(slots, events, X, **kwargs):
"""
A function that calculates the total difference between demand for an event
and the slot capacity it is scheduled in.
"""
overflow = 0
for row, event in enumerate(events):
| python | {
"resource": ""
} |
q8582 | get_initial_array | train | def get_initial_array(events, slots, seed=None):
"""
Obtain a random initial array.
"""
if seed is not None:
| python | {
"resource": ""
} |
q8583 | fcs | train | def fcs(bits):
'''
Append running bitwise FCS CRC checksum to end of generator
'''
fcs = FCS()
for bit in bits:
yield bit
fcs.update_bit(bit)
# test = bitarray()
# for byte in (digest & 0xff, digest >> 8):
# print byte
# for i in range(8):
# b = (byte >> i) & 1 == 1
# test.append(b)
# yield b
# | python | {
"resource": ""
} |
q8584 | modulate | train | def modulate(data):
'''
Generate Bell 202 AFSK samples for the given symbol generator
Consumes raw wire symbols and produces the corresponding AFSK samples.
'''
seconds_per_sample = 1.0 / audiogen.sampler.FRAME_RATE
phase, seconds, bits = 0, 0, 0
# construct generators
clock = (x / BAUD_RATE for x in itertools.count(1))
tones = (MARK_HZ if bit else SPACE_HZ for bit in data)
for boundary, frequency in itertools.izip(clock, tones):
# frequency of | python | {
"resource": ""
} |
q8585 | HackerNews._get_sync | train | def _get_sync(self, url):
"""Internal method used for GET requests
Args:
url (str): URL to fetch
Returns:
Individual URL request's response
Raises:
HTTPError: If HTTP request failed.
"""
| python | {
"resource": ""
} |
q8586 | HackerNews._get_async | train | async def _get_async(self, url, session):
"""Asynchronous internal method used for GET requests
Args:
url (str): URL to fetch
session (obj): aiohttp client session for async loop
Returns:
data (obj): Individual URL request's response corountine
| python | {
"resource": ""
} |
q8587 | HackerNews._async_loop | train | async def _async_loop(self, urls):
"""Asynchronous internal method used to request multiple URLs
Args:
urls (list): URLs to fetch
Returns:
responses (obj): All URL requests' response coroutines
"""
results = []
async with aiohttp.ClientSession(
connector=aiohttp.TCPConnector(ssl=False)
) as session:
for url in urls:
| python | {
"resource": ""
} |
q8588 | HackerNews._run_async | train | def _run_async(self, urls):
"""Asynchronous event loop execution
Args:
urls (list): URLs to fetch
Returns:
results (obj): All URL requests' responses
"""
| python | {
"resource": ""
} |
q8589 | HackerNews.get_item | train | def get_item(self, item_id, expand=False):
"""Returns Hacker News `Item` object.
Fetches the data from url:
https://hacker-news.firebaseio.com/v0/item/<item_id>.json
e.g. https://hacker-news.firebaseio.com/v0/item/69696969.json
Args:
item_id (int or string): Unique item id of Hacker News story,
comment etc.
expand (bool): expand (bool): Flag to indicate whether to
transform all IDs into objects.
Returns:
| python | {
"resource": ""
} |
q8590 | HackerNews.get_items_by_ids | train | def get_items_by_ids(self, item_ids, item_type=None):
"""Given a list of item ids, return all the Item objects
Args:
item_ids (obj): List of item IDs to query
item_type (str): (optional) Item type to filter results with
| python | {
"resource": ""
} |
q8591 | HackerNews.get_user | train | def get_user(self, user_id, expand=False):
"""Returns Hacker News `User` object.
Fetches data from the url:
https://hacker-news.firebaseio.com/v0/user/<user_id>.json
e.g. https://hacker-news.firebaseio.com/v0/user/pg.json
Args:
user_id (string): unique user id of a Hacker News user.
expand (bool): Flag to indicate whether to
transform all IDs into objects.
Returns:
`User` object representing a user on Hacker News.
Raises:
InvalidUserID: If no such user exists on Hacker News.
"""
url = urljoin(self.user_url, F"{user_id}.json")
response = self._get_sync(url)
if not response:
raise InvalidUserID
user = User(response)
if expand and user.submitted:
items = self.get_items_by_ids(user.submitted)
user_opt = {
| python | {
"resource": ""
} |
q8592 | HackerNews.get_users_by_ids | train | def get_users_by_ids(self, user_ids):
"""
Given a list of user ids, return all the User objects
"""
urls = [urljoin(self.user_url, | python | {
"resource": ""
} |
q8593 | HackerNews.top_stories | train | def top_stories(self, raw=False, limit=None):
"""Returns list of item ids of current top stories
Args:
limit (int): specifies the number of stories to be returned.
raw (bool): Flag to indicate whether to | python | {
"resource": ""
} |
q8594 | HackerNews.new_stories | train | def new_stories(self, raw=False, limit=None):
"""Returns list of item ids of current new stories
Args:
limit (int): specifies the number of stories to be returned.
raw (bool): Flag to indicate whether to | python | {
"resource": ""
} |
q8595 | HackerNews.ask_stories | train | def ask_stories(self, raw=False, limit=None):
"""Returns list of item ids of latest Ask HN stories
Args:
limit (int): specifies the number of stories to be returned.
raw (bool): Flag to indicate whether to | python | {
"resource": ""
} |
q8596 | HackerNews.show_stories | train | def show_stories(self, raw=False, limit=None):
"""Returns list of item ids of latest Show HN stories
Args:
limit (int): specifies the number of stories to be returned.
raw (bool): Flag to indicate whether to transform | python | {
"resource": ""
} |
q8597 | HackerNews.job_stories | train | def job_stories(self, raw=False, limit=None):
"""Returns list of item ids of latest Job stories
Args:
limit (int): specifies the number of stories to be returned.
raw (bool): Flag to indicate whether to | python | {
"resource": ""
} |
q8598 | HackerNews.get_max_item | train | def get_max_item(self, expand=False):
"""The current largest item id
Fetches data from URL:
https://hacker-news.firebaseio.com/v0/maxitem.json
Args:
expand (bool): Flag to indicate whether to transform | python | {
"resource": ""
} |
q8599 | HackerNews.get_last | train | def get_last(self, num=10):
"""Returns last `num` of HN stories
Downloads all the HN articles and returns them as Item objects
Returns:
`list` object containing ids of HN stories.
"""
max_item = self.get_max_item()
urls = [urljoin(self.item_url, | python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.