text_prompt
stringlengths 157
13.1k
| code_prompt
stringlengths 7
19.8k
⌀ |
---|---|
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def proxy_for(widget):
"""Create a proxy for a Widget :param widget: A gtk.Widget to proxy This will raise a KeyError if there is no proxy type registered for the widget type. """ |
proxy_type = widget_proxies.get(widget.__class__)
if proxy_type is None:
raise KeyError('There is no proxy type registered for %r' % widget)
return proxy_type(widget) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def update(self, value):
"""Update the widget's value """ |
self.update_internal(value)
self.emit('changed', self.get_widget_value()) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def update_internal(self, value):
"""Update the widget's value without firing a changed signal """ |
self.block()
self.set_widget_value(value)
self.unblock() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def connect_widget(self):
"""Perform the initial connection of the widget the default implementation will connect to the widgets signal based on self.signal_name """ |
if self.signal_name is not None:
# None for read only widgets
sid = self.widget.connect(self.signal_name, self.widget_changed)
self.connections.append(sid) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def add_proxy_for(self, name, widget):
"""Create a proxy for a widget and add it to this group :param name: The name or key of the proxy, which will be emitted with the changed signal :param widget: The widget to create a proxy for """ |
proxy = proxy_for(widget)
self.add_proxy(name, proxy) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _urljoin(left, right):
"""Join two URLs. Takes URLs specified by left and right and joins them into a single URL. If right is an absolute URL, it is returned directly. This differs from urlparse.urljoin() in that the latter always chops off the left-most component of left unless it is trailed by '/', which is not the behavior we want. """ |
# Handle the tricky case of right being a full URL
tmp = urlparse.urlparse(right)
if tmp.scheme or tmp.netloc:
# Go ahead and use urlparse.urljoin()
return urlparse.urljoin(left, right)
# Check for slashes
joincond = (left[-1:], right[:1])
if joincond == ('/', '/'):
# Too many, preserve only one
return left + right[1:]
elif '/' in joincond:
# Just one; great!
return left + right
else:
# Not enough; add one
return left + '/' + right |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def restmethod(method, reluri, *qargs, **headers):
"""Decorate a method to inject an HTTPRequest. Generates an HTTPRequest using the given HTTP method and relative URI. If additional positional arguments are present, they are expected to be strings that name function arguments that should be included as the query parameters of the URL. If additional keyword arguments are present, the keywords are expected to name function arguments and the values are expected to name headers to set from those values. The request is injected as the first function argument after the 'self' argument. Note that two attributes must exist on the object the method is called on: the '_baseurl' attribute specifies the URL that reluri is relative to; and the '_make_req' attribute specifies a method that instantiates an HTTPRequest from a method and full url (which will include query arguments). """ |
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Process the arguments against the original function
argmap, theSelf, req_name = _getcallargs(func, args, kwargs)
# Build the URL
url = _urljoin(theSelf._baseurl, reluri.format(**argmap))
# Build the query string, as needed
if qargs:
query = dict([(k, argmap[k]) for k in qargs
if argmap[k] is not None])
if query:
url += '?%s' % urllib.urlencode(query)
# Build the headers, if needed
hlist = None
if headers:
hlist = hdrs.HeaderDict()
for aname, hname in headers.items():
if argmap[aname]:
hlist[hname] = argmap[aname]
if not hlist:
# If there are no headers, don't send any
hlist = None
# Now, build the request and pass it to the method
argmap[req_name] = theSelf._make_req(method, url,
func.__name__, hlist)
# Call the method
return func(**argmap)
# Return the function wrapper
return wrapper
# Return the actual decorator
return decorator |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def apply_addons(widget, *addon_types, **named_addon_types):
"""Apply some addons to a widget. :param widget: The widget to apply addons to. :param addon_types: A list of addon types, which will be instantiated and applied to the widget with the default name of the addon. :param named_addon_types: A named list of addons, the keywords will be the name of the addon when loaded and will override the default addon name. This can allow loading the same addon multpily for the same widget under different names. Plugins should conform to the GObjectPlugin interface or be a subclass of it. Once loaded, addons will be available as widget.addons.<addon_name> withe standard attribute access. """ |
for addon_type in addon_types:
addon_type(widget)
for name, addon_type in named_addon_types.items():
addon_type(widget, addon_name=name) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def make_path(config, *endings):
""" Create a path based on component configuration. All paths are relative to the component's configuration directory; usually this will be the same for an entire session, but this function supuports component-specific configuration directories. Arguments: config - the configuration object for a component endings - a list of file paths to append to the component's configuration directory """ |
config_dir = config.get("dp.config_dir")
return os.path.join(config_dir, *endings) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def from_json(payload):
""" Convert the JSON expression of a contact information into an instance `Contact. @param payload: a JSON expression representing a contact information: [ type:ContactName, value:string, [is_primary:boolean, [is_verified:boolean]] ] @return: an instance `Contact`. @raise AssertError: if the specified type of this contact information is not a string representation of an item of the enumeration `ContactName`. @raise ValueError: if the value of this contact information is null. """ |
contact_item_number = len(payload)
assert 2 <= contact_item_number <= 4, 'Invalid contact information format'
is_primary = is_verified = None
# Unpack the contact information to each component.
if contact_item_number == 2:
(name, value) = payload
elif contact_item_number == 3:
(name, value, is_primary) = payload
else:
(name, value, is_primary, is_verified) = payload
return Contact(cast.string_to_enum(name, Contact.ContactName), value,
is_primary=is_primary and cast.string_to_boolean(is_primary, strict=True),
is_verified=is_verified and cast.string_to_boolean(is_verified, strict=True)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def from_object(obj):
""" Convert an object representing a contact information to an instance `Contact`. @param obj: an object containg the following attributes: * `name`: an item of the enumeration `ContactName` representing the type of this contact information. * `value`: value of this contact information representing by a string, such as ``+84.01272170781``, the formatted value for a telephone number property. * `is_primary`: indicate whether this contact property is the first to be used to contact the entity that this contact information corresponds to. There is only one primary contact property for a given property name (e.g., `EMAIL`, `PHONE`, `WEBSITE`). * `is_verified`: indicate whether this contact information has been verified, whether it has been grabbed from a trusted Social Networking Service (SNS), or whether through a challenge/response process. @raise ValueError: if the value of this contact information is null. """ |
return obj if isinstance(obj, Contact) \
else Contact(cast.string_to_enum(obj.name, Contact.ContactName),
obj.value,
is_primary=obj.is_primary and cast.string_to_boolean(obj.is_primary, strict=True),
is_verified=obj.is_verified and cast.string_to_boolean(obj.is_verified, strict=True)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def post(self, request, question_level, question_number, format=None):
""" Use this Function to submit. """ |
user = User.objects.get(username=request.user.username)
question = Question.objects.filter(question_level=question_level).filter(question_number=question_number)
if int(user.profile.user_access_level) < int(question_level):
content = {'user_nick':profile.user_nick, 'please move along': 'Nothing to see here' }
return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)
time_last = None
time_last_query = Submission.objects.filter(submission_user__username=request.user.username).filter(submission_question__question_level=question_level).filter(submission_question__question_number=question_number).filter(submission_state='WA').order_by('submission_timestamp').last()
check_resubmission = AcceptedQuestion.objects.filter(record_user=request.user).filter(record_question=question)
if check_resubmission:
content = {'user_nick':profile.user_nick, 'Do not try to resubmit': 'Already Accepted' }
return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)
if time_last_query:
time_last = time_last_query.submission_timestamp
time_limit = self.get_contest_consecutive_submission_halt_time()
# print time_last, time_limit, datetime.datetime.now(), time_limit <= datetime.datetime.now()
if(time_last is None or time_last + time_limit <= datetime.datetime.now()):
type_of_contest = self.get_contest_type()
type_of_submission = self.get_question_type()
if type_of_submission != STRING:
content = { 'user_nick':profile.user_nick, 'please move along': 'WRONG TYPE SUBMISSION'}
return Response(content, status=status.HTTP_405_METHOD_NOT_ALLOWED)
serializer = SubmissionSerializer(data=request.data)
if serializer.is_valid():
serializer.save(submission_user = request.user,
submission_question = self.get_object(question_level,question_number)[0],
)
# checker_queue.delay(int(serializer.data['id']))
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
content = {'user_nick':profile.user_nick, 'Try to submit after some time': 'Nothing to see here' }
return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def post(self, request, question_level, question_number, format=None):
""" Use this Function to submit Comment. """ |
user = User.objects.get(username=request.user.username)
question = Question.objects.filter(question_level=question_level).filter(question_number=question_number)
if int(user.profile.user_access_level) < int(question_level):
content = {'user_nick':profile.user_nick, 'please move along': 'Nothing to see here' }
return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)
time_last = None
time_last_query = Comment.objects.filter(comment_user__username=request.user.username).order_by('comment_timestamp').last()
if time_last_query:
time_last = time_last_query.comment_timestamp
time_limit = self.get_contest_consecutive_submission_halt_time() # the halt time for submission is kept same as submission halt time
# print time_last, time_limit, datetime.datetime.now(), time_limit <= datetime.datetime.now()
if(time_last is None or time_last + time_limit <= datetime.datetime.now()):
serializer = CommentSerializer(data=request.data)
if not request.data['comment_message'] or len(str(request.data['comment_message'])) >= 255:
content = {'user_nick':profile.user_nick, 'Try to submit something small': 'Like your Dick' }
return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE)
if serializer.is_valid():
serializer.save(comment_user = request.user,
comment_question = question[0],
)
checker_queue.delay(int(serializer.data['id']))
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
content = {'user_nick':profile.user_nick, 'Try to submit after some time': 'Nothing to see here' }
return Response(content, status=status.HTTP_406_NOT_ACCEPTABLE) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def calculate_base_sequence(base_offsets):
""" Return a sequence of characters corresponding to the specified base. Example:: '0123456789ABCDEF' @param base_offsets: a list of character offsets in the form of tuples ``(x, y)`` where ``x`` corresponds to the first character of a sequence and ``y`` to the last character of this sequence. @return a sequence of characters, i.e., a string, corresponding to the specified base. """ |
return ''.join([ chr(c) for c in list(itertools.chain.from_iterable(
[ range(ord(x), ord(y) + 1) for (x, y) in base_offsets ])) ]) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def generate_secured_key(value, key_nonce_separator='.', nonce_length=4, base=BASE62):
""" Generate a secured key composed of the integer value encoded in Base62 and a nonce. @param value: an integer value. @param key_nonce_separator: the character that is used to separate the key and the nonce to form the secured key. @param nonce_length: the number of characters to compose the nonce. @param base: a sequence of characters that is used to encode the integer value. @return: a tuple ``(key, nonce, secured_key)``: * ``key``: string representation of the integer value in Base62. * ``nonce``: "number used once", a pseudo-random number to ensure that the key cannot be reused in replay attacks. * ``secured_key``: a string composed of the key concatenated with the nonce. """ |
if not isinstance(value, (int, long)):
raise ValueError()
posix_time = int(time.mktime(datetime.datetime.now().timetuple()))
nonce = hashlib.md5(str(posix_time)).hexdigest()[:nonce_length]
key = int_to_key(value, base=base)
return key, nonce, '%s%s%s' % (key, key_nonce_separator, nonce) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def int_to_key(value, base=BASE62):
""" Convert the specified integer to a key using the given base. @param value: a positive integer. @param base: a sequence of characters that is used to encode the integer value. @return: a key expressed in the specified base. """ |
def key_sequence_generator(value, base):
"""
Generator for producing sequence of characters of a key providing an
integer value and a base of characters for encoding, such as Base62
for instance.
@param value: a positive integer.
@param base: a sequence of characters that is used to encode the
integer value.
@return: the next character of the object's key encoded with the
specified base.
"""
base_length = len(base)
while True:
yield base[value % base_length]
if value < base_length:
break
value /= base_length
return ''.join([ c for c in key_sequence_generator(value, base) ]) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def key_to_int(key, base=BASE62):
""" Convert the following key to an integer. @param key: a key. @param base: a sequence of characters that was used to encode the integer value. @return: the integer value corresponding to the given key. @raise ValueError: if one character of the specified key doesn't match any character of the specified base. """ |
base_length = len(base)
value = 0
for c in reversed(key):
value = (value * base_length) + base.index(c)
return value |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def parse_secured_key(secured_key, key_nonce_separator='.', nonce_length=4, base=BASE62):
""" Parse a given secured key and return its associated integer, the key itself, and the embedded nonce. @param secured_key a string representation of a secured key composed of a key in Base62, a separator character, and a nonce. @param key_nonce_separator: the character that is used to separate the key and the nonce to form the secured key. @param nonce_length: the number of characters to compose the nonce. @param base: a sequence of characters that is used to encode the integer value. @return: a tuple ``(value, key, nonce)``: * ``value``: the integer value of the key. * ``key``: the plain-text key. * ``nonce``: "number used once", a pseudo-random number to ensure that the key cannot be reused in replay attacks. @raise ValueError: if the format of the secured key is invalid, or if the embedded nonce is of the wrong length. """ |
parts = secured_key.split(key_nonce_separator)
if len(parts) != 2:
raise ValueError('Invalid secured key format')
(key, nonce) = parts
if len(nonce) != nonce_length:
raise ValueError('Invalid length of the key nonce')
return key_to_int(key, base=base), key, nonce |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get(args):
""" Get a template by name. """ |
m = TemplateManager(args.hosts)
t = m.get(args.name)
if t:
print(json.dumps(t, indent=2))
else:
sys.exit(1) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def delete(args):
""" Delete a template by name """ |
m = TemplateManager(args.hosts)
m.delete(args.name) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def user_parser(user):
""" Parses a user object """ |
if __is_deleted(user):
return deleted_parser(user)
if user['id'] in item_types:
raise Exception('Not a user name')
if type(user['id']) == int:
raise Exception('Not a user name')
return User(
user['id'],
user['delay'],
user['created'],
user['karma'],
user['about'],
user['submitted'],
) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def story_parser(story):
""" Parses a story object """ |
if __is_deleted(story):
return deleted_parser(story)
if story['type'] != 'story':
return item_parser(story)
return Story(
__check_key('id', story),
__check_key('by', story),
__check_key('kids', story),
__check_key('score', story),
__check_key('time', story),
__check_key('title', story),
__check_key('type', story),
__check_key('url', story),
) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def comment_parser(comment):
""" Parses a comment object """ |
try:
if comment['type'] != 'comment':
return item_parser(comment)
except KeyError:
return deleted_parser(comment)
return Comment(
comment['id'],
comment['by'],
__check_key('kids', comment), # some comments do not have kids key
comment['parent'],
comment['text'],
comment['time'],
comment['type'],
) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def poll_parser(poll):
""" Parses a poll object """ |
if __is_deleted(poll):
return deleted_parser(poll)
if poll['type'] not in poll_types:
raise Exception('Not a poll type')
return Poll(
poll['id'],
poll['by'],
__check_key('kids', poll), # poll and pollopt differ this property
__check_key('parts', poll), # poll and pollopt differ this property
poll['score'],
poll['text'],
poll['time'],
poll['title'],
poll['type'],
) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def item_parser(item):
""" Parses a item object It gives None for the property not present Used when some item types do not get parsed easily when using gevent """ |
if __is_deleted(item):
return deleted_parser(item)
return Item(
__check_key('id', item),
__check_key('deleted', item),
__check_key('type', item),
__check_key('by', item),
__check_key('time', item),
__check_key('text', item),
__check_key('dead', item),
__check_key('parent', item),
__check_key('kids', item),
__check_key('url', item),
__check_key('score', item),
__check_key('title', item),
__check_key('parts', item),
) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def json_to_string(value, null_string_repr='[]', trimable=False):
""" Return a string representation of the specified JSON object. @param value: a JSON object. @param null_string_rep: the string representation of the null object. @return: a string representation of the specified JSON object. """ |
return null_string_repr if is_undefined(value) \
else obj.jsonify(value, trimable=trimable) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def string_to_boolean(value, strict=False, default_value=False):
""" Return a boolean with a value represented by the specified string. @param value: a string representation of a boolean. @param strict: indicate whether the specified string MUST be of a valid boolean representation. @return: the boolean value represented by the string. @raise ValueError: if the string doesn't represent a valid boolean, while the argument ``strict`` equals ``True``. """ |
if is_undefined(value) and default_value:
return default_value
if isinstance(value, bool):
return value
if isinstance(value, basestring) and value is not None:
value = value.lower()
is_true = value in ('yes', 'true', 't', '1')
if not is_true and strict == True and \
value not in ('no', 'false', 'f', '0'):
raise ValueError("The specified string doesn't represent a boolean value")
return is_true |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def string_to_date(value):
""" Return a Python date that corresponds to the specified string representation. @param value: string representation of a date. @return: an instance ``datetime.datetime`` represented by the string. """ |
if isinstance(value, datetime.date):
return value
return dateutil.parser.parse(value).date() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def string_to_decimal(value, strict=True):
""" Return a decimal corresponding to the string representation of a number. @param value: a string representation of an decimal number. @param strict: indicate whether the specified string MUST be of a valid decimal number representation. @return: the decimal value represented by the string. @raise ValueError: if the string doesn't represent a valid decimal, while the argument ``strict`` equals ``True``. """ |
if is_undefined(value):
if strict:
raise ValueError('The value cannot be null')
return None
try:
return float(value)
except ValueError:
raise ValueError(
'The specified string "%s" does not represent an integer' % value) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def string_to_enum(value, enumeration, strict=True, default_value=None):
""" Return the item of an enumeration that corresponds to the specified string representation. @param value: string representation of an item of a Python enumeration. @param enumeration: a Python enumeration. @param strict: indicate whether the value must correspond to an item of the specified Python enumeration or if ``None`` value is accepted. @return: the item of the Python enumeration the specified string representation corresponds to. @raise ValueError: if the enumeration is not an instance of ``Enum``, or if the string representation doesn't correspond to any item of the given Python enumeration, or if the default value is not an item of the given Python enumeration. """ |
if not isinstance(enumeration, Enum):
raise ValueError('The specified enumeration is not an instance of Enum')
if is_undefined(value):
if strict:
raise ValueError('The value cannot be null')
if default_value is not None and not default_value in enumeration:
raise ValueError('The default value must be an item of the specified enumeration')
return default_value
item = [ item for item in enumeration if str(item) == value]
if len(item) == 0:
raise ValueError('The specified string "%s" does not represent any item of the enumeration' % value)
return item[0] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def string_to_integer(value, strict=False):
""" Return an integer corresponding to the string representation of a number. @param value: a string representation of an integer number. @param strict: indicate whether the specified string MUST be of a valid integer number representation. @return: the integer value represented by the string. @raise ValueError: if the string doesn't represent a valid integer, while the argument ``strict`` equals ``True``. """ |
if is_undefined(value):
if strict:
raise ValueError('The value cannot be null')
return None
try:
return int(value)
except ValueError:
raise ValueError('The specified string "%s" does not represent an integer' % value) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def string_to_ipv4(value, strict=False):
""" Return a tuple corresponding to the string representation of an IPv4 address. An IPv4 address is canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots, e.g., ``172.16.254.1``. @param value: a dotted-decimal notation of an IPv4 address, consisting of four decimal numbers, each ranging from ``0`` to ``255``, separated by dots. @param strict: indicate whether the ``None`` value is accepted. @return: a tuple of four decimal numbers ``(byte1, byte2, byte3, byte4)``, each ranging from ``0`` to ``255``. """ |
if is_undefined(value):
if strict:
raise ValueError('The value cannot be null')
return None
if not REGEX_IPV4.match(value):
raise ValueError('The specified string "%s" does not represent a IPv4' % value)
ipv4 = [ int(byte) for byte in value.split('.') if int(byte) < 256]
if len(ipv4) != 4:
raise ValueError('The IPv4 "%s" has invalid byte(s)' % value)
return ipv4 |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def string_to_time(value):
""" Return an instance ``datetime.time`` that corresponds to the specified string representation. @param value: a string representation of a time ``HH:MM[:SS]``, where: * ``HH``: hour (24-hour clock) as a zero-padded decimal number. * ``MM``: minute as a zero-padded decimal number. * ``SS``: second as a zero-padded decimal number. @return: an instance ``datetime.time`` represented by the string. @raise ValueError: if the string representation doesn't correspond to a valid time. """ |
try:
return datetime.datetime.strptime(value, '%H:%M:%S').time()
except ValueError:
return datetime.datetime.strptime(value, '%H:%M').time() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def string_to_timestamp(value, second_digits=3, default_utc_offset=None, rounding_method=TimestampRoundingMethod.ceiling):
""" Return the ISO 8601 date time that corresponds to the specified string representation. When the required precision is lower than microsecond, the function rounds the sub-second time to the specified number of digits. By default, when possible, it returns the smallest value greater than or equal to the sub-second time for specified precision, also know as "ceiling conversion"; for instance, the function converts ``918845`` microseconds to ``919`` milliseconds. Otherwise, the function return the largest value equal to the sub-second time for specified precision, also known as "floor conversion"; for instance, the function converts ``999916`` microseconds to ``999`` milliseconds. @param value: string representation of an ISO date time. @param second_digits: indicate the number of second digits after the decimal point, to determine the time precision. For instance, 3 digits corresponds to milliseconds, 6 digits corresponds to microseconds. @param default_utc_offset: if the specified string representation doesn't mention any time zone, use this offset as the time zone of the corresponding ISO 8601 date time, or use the time zone of the machine this code is running on as the default time zone. @param rounding_method: one of the rounding method, as declared in ``TimestampRoundingMethod``, when the required precision is lower than microsecond. @return: an instance ``ISO8601DateTime`` represented by the string. @raise ValueError: if the specified number of second digits after the decimal point is not in 0..6, or if the string representation doesn't correspond to a valid ISO 8601 date time. """ |
if second_digits < 0 or second_digits > 6:
raise ValueError('The number of second digits after the decimal point must be in 0..6')
if not value:
return None
if isinstance(value, datetime.date):
pydatetime = ISO8601DateTime.from_datetime(value)
else:
if default_utc_offset is not None:
value = '%s%s%02d' % (value[:19], '-' if default_utc_offset < 0 else '+', default_utc_offset)
d = dateutil.parser.parse(value)
if second_digits >= 6:
microsecond = d.microsecond
else:
f = 10 ** (6 - second_digits)
if rounding_method == TimestampRoundingMethod.ceiling:
m = int(math.ceil(float(d.microsecond) / f))
if m >= f:
m -= 1
else:
m = int(math.floor(float(d.microsecond) / f))
microsecond = m * f
pydatetime = ISO8601DateTime(
d.year, d.month, d.day,
d.hour, d.minute, d.second, microsecond,
d.tzinfo or dateutil.tz.tzlocal())
pydatetime.set_second_digits(second_digits)
return pydatetime |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def create_app(debug=False):
""" Create the flask app :param debug: Use debug mode :type debug: bool :return: Created app :rtype: flask.Flask """ |
app = Flask(__name__)
app.secret_key = str(uuid.uuid4())
app.json_encoder = DateTimeEncoder
app.register_blueprint(page)
Bower(app)
api = Api(app)
api.add_resource(
PluginListAPI,
api_path + "plugins/",
endpoint="APIPlugins"
)
api.add_resource(
PluginAPI,
api_path + "plugins/<plugin_key>",
endpoint="APIPlugin"
)
api.add_resource(
PluginResourceAPI,
api_path + "plugins/<plugin_key>/resources/",
endpoint="APIPluginResource"
)
if debug:
# Setup app for real debuging mode
app.debug = True
# Force update of static files (even in dev mode, browsers still cache)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
@app.after_request
def add_header(response):
response.headers['Cache-Control'] = "public, max-age=0"
return response
return app |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def plugin(plugin_key=None):
""" Serve plugin page :param plugin_key: Which plugin page to server; if None -> landing page (default: None) :type plugin_key: None | unicode """ |
p = None
if plugin_key:
try:
p = db_plugin_instance.plugin_get(plugin_key)
except KeyError:
logger.error(u"Plugin '{}' not found".format(plugin_key))
return render_template("plugin.html", title="Plugins", plugin=p) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def on_segment(point_p, point_q, point_r):
""" Given three colinear points p, q, r, the function checks if point q lies on line segment "pr" :param point_p: :type point_p: models.Point :param point_q: :type point_q: models.Point :param point_r: :type point_r: models.Point :return: if point r on line segment "pr" :rtype: bool """ |
if (point_q.x <= max(point_p.x, point_r.x)
and point_q.x >= min(point_p.x, point_r.x)
and point_q.y <= max(point_p.y, point_r.y)
and point_q.y >= min(point_p.y, point_r.y)):
return True
return False |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def is_intersect(line_a, line_b):
""" Determine if lina_a intersect with line_b :param lina_a: :type lina_a: models.Line :param lina_b: :type line_b: models.Line :return: :rtype: bool """ |
# Find the four orientations needed for general and special cases
orientation_1 = orientation(line_a.endpoint_a, line_a.endpoint_b,
line_b.endpoint_a)
orientation_2 = orientation(line_a.endpoint_a, line_a.endpoint_b,
line_b.endpoint_b)
orientation_3 = orientation(line_b.endpoint_a, line_b.endpoint_b,
line_a.endpoint_a)
orientation_4 = orientation(line_b.endpoint_a, line_b.endpoint_b,
line_a.endpoint_b)
# General case
if (orientation_1 != orientation_2 and orientation_3 != orientation_4):
return True
# Special cases
if (orientation_1 == 0 and on_segment(line_a.endpoint_a, line_b.endpoint_a,
line_a.endpoint_b)):
return True
if (orientation_2 == 0 and on_segment(line_a.endpoint_a, line_b.endpoint_b,
line_a.endpoint_b)):
return True
if (orientation_3 == 0 and on_segment(line_b.endpoint_a, line_a.endpoint_a,
line_b.endpoint_b)):
return True
if (orientation_4 == 0 and on_segment(line_b.endpoint_a, line_a.endpoint_b,
line_b.endpoint_b)):
return True
return False |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def is_inside(point, region):
""" Detemine if point is in region :param point: :type point: models.Point :param region: :type region: Region """ |
points = region.vertices
extrame = models.Point(x=1000000, y=point.y)
points = points + [points[0]]
intersect_count = 0
for i in range(len(points) - 1):
if is_intersect(models.Line(point, extrame),
models.Line(points[i], points[i+1])):
intersect_count += 1
return intersect_count % 2 == 1 |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def analyze(qpi, r0, method="edge", model="projection", edgekw={}, imagekw={}, ret_center=False, ret_pha_offset=False, ret_qpi=False):
"""Determine refractive index and radius of a spherical object Parameters qpi: qpimage.QPImage Quantitative phase image data r0: float Approximate radius of the sphere [m] method: str The method used to determine the refractive index can either be "edge" (determine the radius from the edge detected in the phase image) or "image" (perform a 2D phase image fit). model: str The light-scattering model used by `method`. If `method` is "edge", only "projection" is allowed. If `method` is "image", `model` can be one of "mie", "projection", "rytov", or "rytov-sc". edgekw: dict Keyword arguments for tuning the edge detection algorithm, see :func:`qpsphere.edgefit.contour_canny`. imagekw: dict Keyword arguments for tuning the image fitting algorithm, see :func:`qpsphere.imagefit.alg.match_phase` ret_center: bool If True, return the center coordinate of the sphere. ret_pha_offset: bool If True, return the phase image background offset. ret_qpi: bool If True, return the modeled data as a :class:`qpimage.QPImage`. Returns ------- n: float Computed refractive index r: float Computed radius [m] c: tuple of floats Only returned if `ret_center` is True; Center position of the sphere [px] pha_offset: float Only returned if `ret_pha_offset` is True; Phase image background offset qpi_sim: qpimage.QPImage Only returned if `ret_qpi` is True; Modeled data Notes ----- If `method` is "image", then the "edge" method is used as a first step to estimate initial parameters for radius, refractive index, and position of the sphere using `edgekw`. If this behavior is not desired, please make use of the method :func:`qpsphere.imagefit.analyze`. """ |
if method == "edge":
if model != "projection":
raise ValueError("`method='edge'` requires `model='projection'`!")
n, r, c = edgefit.analyze(qpi=qpi,
r0=r0,
edgekw=edgekw,
ret_center=True,
ret_edge=False,
)
res = [n, r]
if ret_center:
res.append(c)
if ret_pha_offset:
res.append(0)
if ret_qpi:
qpi_sim = simulate(radius=r,
sphere_index=n,
medium_index=qpi["medium index"],
wavelength=qpi["wavelength"],
grid_size=qpi.shape,
model="projection",
pixel_size=qpi["pixel size"],
center=c)
res.append(qpi_sim)
elif method == "image":
n0, r0, c0 = edgefit.analyze(qpi=qpi,
r0=r0,
edgekw=edgekw,
ret_center=True,
ret_edge=False,
)
res = imagefit.analyze(qpi=qpi,
model=model,
n0=n0,
r0=r0,
c0=c0,
imagekw=imagekw,
ret_center=ret_center,
ret_pha_offset=ret_pha_offset,
ret_qpi=ret_qpi
)
else:
raise NotImplementedError("`method` must be 'edge' or 'image'!")
return res |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def bg_phase_mask_from_sim(sim, radial_clearance=1.1):
"""Return the background phase mask of a qpsphere simulation Parameters sim: qpimage.QPImage Quantitative phase data simulated with qpsphere; The simulation keyword arguments "sim center", "sim radius", and "pixel size" must be present in `sim.meta`. radial_clearance: float Multiplicator to the fitted radius of the sphere; modifies the size of the mask; set to "1" to use the radius determined by :func:`qpsphere.analyze`. The circular area containing the phase object is set to `False` in the output `mask` image. Returns ------- mask: boolean 2d np.ndarray The mask is `True` for background regions and `False` for object regions. """ |
# Mask values around the object
cx, cy = sim["sim center"]
radius = sim["sim radius"]
px_um = sim["pixel size"]
x = np.arange(sim.shape[0]).reshape(-1, 1)
y = np.arange(sim.shape[1]).reshape(1, -1)
rsq = (x - cx)**2 + (y - cy)**2
mask = rsq > (radius/px_um * radial_clearance)**2
return mask |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def bg_phase_mask_for_qpi(qpi, r0, method="edge", model="projection", edgekw={}, imagekw={}, radial_clearance=1.1):
"""Determine the background phase mask for a spherical phase object The position and radius of the phase object are determined with :func:`analyze`, to which the corresponding keyword arguments are passed. A binary mask is created from the simulation results via :func:`bg_phase_mask_from_sim`. Parameters qpi: qpimage.QPImage Quantitative phase image data r0: float Approximate radius of the sphere [m] method: str The method used to determine the refractive index can either be "edge" (determine the radius from the edge detected in the phase image) or "image" (perform a 2D phase image fit). model: str The light-scattering model used by `method`. If `method` is "edge", only "projection" is allowed. If `method` is "image", `model` can be one of "mie", "projection", "rytov", or "rytov-sc". edgekw: dict Keyword arguments for tuning the edge detection algorithm, see :func:`qpsphere.edgefit.contour_canny`. imagekw: dict Keyword arguments for tuning the image fitting algorithm, see :func:`qpsphere.imagefit.alg.match_phase` radial_clearance: float Multiplicator to the fitted radius of the sphere; modifies the size of the mask; set to "1" to use the radius determined by :func:`qpsphere.analyze`. The circular area containing the phase object is set to `False` in the output `mask` image. Returns ------- mask: boolean 2d np.ndarray The mask is `True` for background regions and `False` for object regions. """ |
# fit sphere
_, _, sim = analyze(qpi=qpi,
r0=r0,
method=method,
model=model,
edgekw=edgekw,
imagekw=imagekw,
ret_qpi=True)
# determine mask
mask = bg_phase_mask_from_sim(sim=sim,
radial_clearance=radial_clearance)
return mask |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def with_subtype(self, subtype):
"""Add a subtype segment Args: subtype (str):
May be one of ``['a', 'c', 'r']``. See the `Reference Definition <https://elections.democracyclub.org.uk/reference_definition>`_. for valid election type/subtype combinations. Returns: IdBuilder Raises: ValueError """ |
self._validate_subtype(subtype)
self.subtype = subtype
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def with_organisation(self, organisation):
"""Add an organisation segment. Args: organisation (str):
Official name of an administrative body holding an election. Returns: IdBuilder Raises: ValueError """ |
if organisation is None:
organisation = ''
organisation = slugify(organisation)
self._validate_organisation(organisation)
self.organisation = organisation
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def with_division(self, division):
"""Add a division segment Args: division (str):
Official name of an electoral division. Returns: IdBuilder Raises: ValueError """ |
if division is None:
division = ''
division = slugify(division)
self._validate_division(division)
self.division = division
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def execute(cmd):
''' Call cmd and return output. return None if any exception occurs '''
try:
return safely_decode(check_output(cmd))
except Exception as e:
logger.warn(u'Couldnt execute cmd: %s.\nReason: %s' % (cmd, e))
return None |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def create_environment(component_config):
""" Create a modified environment. Arguments component_config - The configuration for a component. """ |
ret = os.environ.copy()
for env in component_config.get_list("dp.env_list"):
real_env = env.upper()
value = os.environ.get(real_env)
value = _prepend_env(component_config, env, value)
value = _append_env(component_config, env, value)
_apply_change(ret, real_env, value, component_config)
return ret |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def set_scale_alpha_from_selection(self):
'''
Set scale marker to alpha for selected layer.
'''
# 1. Look up selected layer.
selection = self.treeview_layers.get_selection()
list_store, selected_iter = selection.get_selected()
# 2. Set adjustment to current alpha value for selected layer (if any).
if selected_iter is None:
# No layer was selected, so disable scale widget.
self.adjustment_alpha.set_value(100)
self.scale_alpha.set_sensitive(False)
return
else:
surface_name, alpha = list_store[selected_iter]
self.adjustment_alpha.set_value(alpha * 100)
self.scale_alpha.set_sensitive(True) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def set_alpha_for_selection(self, alpha):
'''
Set alpha for selected layer.
'''
# 1. Look up selected layer.
selection = self.treeview_layers.get_selection()
list_store, selected_iter = selection.get_selected()
# 2. Set alpha value for layer (if selected).
if selected_iter is None:
# No layer was selected, so disable scale widget.
return
else:
surface_name, original_alpha = list_store[selected_iter]
self.set_alpha(surface_name, alpha)
self.set_scale_alpha_from_selection() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def process_data(self, block):
"""expects Block from Compressor""" |
if hasattr(block, 'send_destinations') and block.send_destinations:
self.fire(events.FileProcessed(block))
self._log_in_db(block)
if self._sent_log_file:
self._log_in_sent_log(block)
self.log.info("Sent to '%s' file '%s' containing files: %s",
str(block.send_destinations),
block.processed_data_file_info.basename,
str([file_info.path for file_info in block.content_file_infos]))
else:
self.log.info("File %s wasn't sent", block.processed_data_file_info.basename)
return block |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def should_send(self, request):
"""Returns whether or not the request should be sent to the modules, based on the filters.""" |
if self.filters.get('whitelist', None):
return request.tree.type in self.filters['whitelist']
elif self.filters.get('blacklist', None):
return request.tree.type not in self.filters['blacklist']
else:
return True |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get(args):
""" Get a river by name. """ |
m = RiverManager(args.hosts)
r = m.get(args.name)
if r:
print(json.dumps(r, indent=2))
else:
sys.exit(1) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def create(args):
""" Create a river. This command expects to be fed a JSON document on STDIN. """ |
data = json.load(sys.stdin)
m = RiverManager(args.hosts)
m.create(args.name, data) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def delete(args):
""" Delete a river by name """ |
m = RiverManager(args.hosts)
m.delete(args.name) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def compare(args):
""" Compare the extant river with the given name to the passed JSON. The command will exit with a return code of 0 if the named river is configured as specified, and 1 otherwise. """ |
data = json.load(sys.stdin)
m = RiverManager(args.hosts)
if m.compare(args.name, data):
sys.exit(0)
else:
sys.exit(1) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def analyze(qpi, model, n0, r0, c0=None, imagekw={}, ret_center=False, ret_pha_offset=False, ret_qpi=False):
"""Fit refractive index and radius to a phase image of a sphere Parameters qpi: QPImage Quantitative phase image information model: str Name of the light-scattering model (see :const:`qpsphere.models.available`) n0: float Approximate refractive index of the sphere r0: float Approximate radius of the sphere [m] c0: tuple of (float, float) Approximate center position in ndarray index coordinates [px]; if set to `None` (default), the center of the image is used. imagekw: dict Additional keyword arguments to :func:`qpsphere.imagefit.alg.match_phase`. ret_center: bool Return the center coordinate of the sphere ret_pha_offset: bool If True, return the phase image background offset. ret_qpi: bool If True, return the modeled data as a :class:`qpimage.QPImage`. Returns ------- n: float Computed refractive index r: float Computed radius [m] c: tuple of floats Only returned if `ret_center` is True Center position of the sphere [px] pha_offset: float Only returned if `ret_pha_offset` is True Phase image background offset qpi_sim: qpimage.QPImage Only returned if `ret_qpi` is True Modeled data """ |
res = match_phase(qpi, model=model, n0=n0, r0=r0, c0=c0,
ret_center=ret_center,
ret_pha_offset=ret_pha_offset,
ret_qpi=ret_qpi,
**imagekw)
return res |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def rm_tempdirs():
''' Remove temporary build folders '''
tempdirs = [Dir.BUILD, Dir.COCOA_BUILD, Dir.LIB]
for tempdir in tempdirs:
if os.path.exists(tempdir):
shutil.rmtree(tempdir, ignore_errors=True) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def reformat_pattern(pattern, compile=False):
'''
Apply the filters on user pattern to generate a new regular expression
pattern.
A user provided variable, should start with an alphabet, can be
alphanumeric and can have _.
'''
# User pattern: (w:<name>) --> Changes to (?P<name>\w)
rex_pattern = re.sub(r'\(w:<([\w\d_]+)>\)', '(?P<\\1>\w+)', pattern)
# User pattern: (d:<name>) --> change to (?P<name>\d)
rex_pattern = re.sub(r'\(d:<([\w\d_]+)>\)', '(?P<\\1>\d+)', rex_pattern)
# User pattern: (W:<name>) --> change to (?P<name>\w)
rex_pattern = re.sub(r'\(W:<([\w\d_]+)>\)', '(?P<\\1>\W+)', rex_pattern)
# User pattern: (any:<anyvalue> --> change to (?P<anyvalue>.*)
rex_pattern = re.sub(r'\(any:<([\w\d_]+)>\)', '(?P<\\1>.*)', rex_pattern)
# User pattern: (ip:<ipaddr>) --> Change to (?P<ipaddr>\d+\.\d+\.\d+\.\d+)
rex_pattern = re.sub(r'\(ip:<([\w\d_]+)>\)',
'(?P<\\1>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})',
rex_pattern)
# User pattern: (mac:<macaddr>) --> Change to (?P<mac>\w\w:\w\w:\w\w:..)
rex_pattern = re.sub(r'\(mac:<([\w\d_]+)>\)',
'(?P<\\1>\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)', rex_pattern)
# User pattern: (decimal:<name>) --> Change to (?P<name>\d*\.\d+|\d+)
rex_pattern = re.sub(r'\(decimal:<([\w\d_]+)>\)',
'(?P<\\1>\d*\.\d+|\d+)', rex_pattern)
# User pattern: (measurement:<name> --> change to \
# (?P<name>\d*\.\d+|\d+)(\w+|\w+/\w+)
rex_pattern = re.sub(r'\(measurement:<([\w\d_]+)>\)',
'(?P<\\1>\d*\.\d+|\d+)(?P<\\1_unit>\w+|\w+/\w+)',
rex_pattern)
######################################
# Timestamp patterns.
# User pattern: (ts[n]:<timestamp>) -->
# Converted to: The below options.
######################################
# User pattern: (ts1:<timestamp>)
# Keystone, nova, libvirt, cinder
# Example: 2015-11-13 06:38:04.571
rex_pattern = re.sub(
r'\(ts1:<([\w\d_]+)>\)',
'(?P<\\1>(\d{4})-(\d{1,2})-(\d{1,2}) (\d+):(\d+):(\d+)\.(\d+))',
rex_pattern)
# User pattern: (ts2:<timestamp>)
# contrail
# Example: 2015-11-13 Fri 13:14:51:907.395 PST
rex_pattern = re.sub(
r'\(ts2:<([\w\d_]+)>\)',
'(?P<\\1>(\d{4})-(\d{1,2})-(\d{1,2}) (\w{3}) (\d+):(\d+):(\d+):(\d+)\.(\d+))',
rex_pattern)
# User pattern: (ts3:<timestamp>)
# apache2
# Example: Thu Aug 20 14:18:34 2015
rex_pattern = re.sub(
r'\(ts3:<([\w\d_]+)>\)',
'(?P<\\1>(\w{3}) (\w{3}) (\d{1,2}) (\d+):(\d+):(\d+) (\d{4}))',
rex_pattern)
# User pattern: (ts4:<timestamp>)
# Example: 02/Nov/2015:09:03:19 -0800
rex_pattern = re.sub(
r'\(ts4:<([\w\d_]+)>\)',
'(?P<\\1>(\d+)\/(\w{3})\/(\d{4}):(\d+):(\d+):(\d+) -(\d+))',
rex_pattern)
# User pattern: (ts5:<timestamp>)
# ceph logs.
# Example: 2015-11-13 06:25:29.436844
rex_pattern = re.sub(
r'\(ts5:<([\w\d_]+)>\)',
'(?P<\\1>(\d{4})-(\d{1,2})-(\d{1,2}) (\d+):(\d+):(\d+)\.(\d+))',
rex_pattern)
# User pattern: (ts6:<timestamp>)
# cassandra
# Example:2015-10-23 12:38:15
rex_pattern = re.sub(
r'\(ts6:<([\w\d_]+)>\)',
'(?P<\\1>(\d{4})-(\d{1,2})-(\d{1,2}) (\d+):(\d+):(\d+))',
rex_pattern)
# User pattern: (ts7:<timestamp>)
# haproxy
# Example: 13/Nov/2015:06:25:05.465
rex_pattern = re.sub(
r'\(ts7:<([\w\d_]+)>\)',
'(?P<\\1>(\d+)\/(\w{3})\/(\d{4}):(\d+):(\d+):(\d+)\.(\d+))',
rex_pattern)
# User pattern: (ts8:<timestamp>)
# mysql
# Example: 12:03:28
rex_pattern = re.sub(
r'\(ts8:<([\w\d_]+)>\)',
'(?P<\\1>(\d+):(\d+):(\d+))',
rex_pattern)
# User pattern: (ts9:<timestamp>)
# reddis
# Example: 08 Nov 06:26:05.084
rex_pattern = re.sub(
r'\(ts9:<([\w\d_]+)>\)',
'(?P<\\1>(\d+) (\w{3}) (\d+):(\d+):(\d+)\.(\d+))',
rex_pattern)
# user pattern: (ts10:<timestamp>)
# supervisord, zookeeper
# Example: 2015-06-30 10:59:18,133
rex_pattern = re.sub(
r'\(ts10:<([\w\d_]+)>\)',
'(?P<\\1>(\d{4})-(\d{1,2})-(\d{1,2}) (\d+):(\d+):(\d+),(\d+))',
rex_pattern)
# User pattern: (ts11:<timestamp>)
# dmesg
# Example: 11148214.574583
rex_pattern = re.sub(
r'\(ts11:<([\w\d_]+)>\)',
'(?P<\\1>(\d+)\.(\d+))',
rex_pattern)
# Finally if no prefix is specified take default action.
rex_pattern = re.sub(r'\(<([\w\d_]+)>\)', '(?P<\\1>.*)', rex_pattern)
if compile:
return re.compile(rex_pattern)
return rex_pattern |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def match_string(pattern, search_string):
'''
Match a pattern in a string
'''
rexobj = REX(pattern, None)
rexpatstr = reformat_pattern(pattern)
#print "rexpatstr: ", rexpatstr
rexpat = re.compile(rexpatstr)
rexobj.rex_patternstr = rexpatstr
rexobj.rex_pattern = rexpat
line_count = 1
for line in search_string.splitlines():
line = line.strip()
mobj = rexpat.match(line)
if mobj:
populate_resobj(rexobj, mobj, line_count)
line_count += 1
return rexobj |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def populate_resobj(rexobj, mobj, loc):
'''
Popuate the result object and append it to the
rexobj results.
'''
resobj = REXResult(mobj, loc)
rexobj.matches.append(resobj)
rexobj.res_count += 1 |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def match_file(pattern, filename):
'''
The function will match a pattern in a file and return
a rex object, which will have all the matches found in the file.
'''
# Validate user data.
if pattern is None:
return None
if os.stat(filename).st_size == 0:
return None
rexobj = REX(pattern, filename)
rexpatstr = reformat_pattern(pattern)
#print "rexpatstr: ", rexpatstr
rexpat = re.compile(rexpatstr)
rexobj.rex_patternstr = rexpatstr
rexobj.rex_pattern = rexpat
sfile = open(filename, 'r')
data = sfile.read()
sfile.close()
line_count = 1
for line in data.splitlines():
mobj = rexpat.match(line)
if mobj:
populate_resobj(rexobj, mobj, line_count)
line_count += 1
return rexobj |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def parse_tabular_string(search_string,
header_keys,
delimiter=None,
merge_list=None):
'''
Given a string in a tabular format, parse it and return a
dictionary
@args:
search_string: This is a string in tabular format (e.g.: output of df
command)
header_keys: This is a list of strings for the headers.
delimiter(optional): Default is None, which translates to spaces
merge_list(optional): In some cases 2 fields need to be merged as they
are one value.
'''
first_line = True
parsed_results = []
for line in search_string.splitlines():
if first_line:
first_line = False
else:
result = {}
row = line.split()
if merge_list:
for mergeset in merge_list:
fidx = mergeset[0]
lidx = mergeset[1]
try:
row[fidx] = "_".join(row[fidx:(lidx+1)])
row.remove(row[lidx])
except IndexError:
pass
if len(row) != len(header_keys):
print "Incorrect fields len "
continue
key_count = 0
for column in row:
result[header_keys[key_count]] = column
key_count += 1
parsed_results.append(result)
return parsed_results |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def dump_rexobj_results(rexobj, options=None):
'''
print all the results.
'''
print("-" * 60)
print("Match count: ", rexobj.res_count)
matches = rexobj.matches
for match in matches:
print("Loc:", match.loc, ":: ")
for key in match.named_groups.keys():
print("%s: %s" %
(key, match.named_groups[key]))
print("") |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def get_match_value(rexobj, key, index=0):
'''
Return a matched value for the key for a specific match from the
results.
'''
if rexobj is None:
return None
if rexobj.res_count == 0:
return None
try:
return rexobj.matches[index].named_groups[key]
except IndexError:
return None
except KeyError:
return None |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def save_resource(plugin_name, resource_name, resource_data):
""" Save a resource in local cache :param plugin_name: Name of plugin this resource belongs to :type plugin_name: str :param resource_name: Name of resource :type resource_name: str :param resource_data: Resource content - base64 encoded :type resource_data: str :rtype: None """ |
path = os.path.join(resource_dir_path, plugin_name)
if not os.path.exists(path):
os.makedirs(path)
path = os.path.join(path, resource_name)
logger.debug("Saving {}".format(path))
with open(path, 'wb') as f:
f.write(base64.b64decode(resource_data)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def __convert_env(env, encoding):
"""Environment variables should be bytes not unicode on Windows.""" |
d = dict(os.environ, **(oget(env, {})))
# workaround for Windows+Python3 environment
if not SHOULD_NOT_ENCODE_ARGS:
return dict((k.encode(encoding), v.encode(encoding)) for k, v in d.items())
else:
return d |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def length(self):
""" Calculate total length of the polyline :return: total length in meters :rtype: float """ |
total_length = 0
for location_a, location_b in zip(
self.locations[:-1], self.locations[1:]):
total_length += Line(location_a, location_b).length
return total_length |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def install_hook(dialog=SimpleExceptionDialog, invoke_old_hook=False, **extra):
""" install the configured exception hook wrapping the old exception hook don't use it twice :oparam dialog: a different exception dialog class :oparam invoke_old_hook: should we invoke the old exception hook? """ |
global _old_hook
assert _old_hook is None
def new_hook(etype, eval, trace):
gobject.idle_add(dialog_handler, dialog, etype, eval, trace, extra)
if invoke_old_hook:
_old_hook(etype, eval, trace)
_old_hook = sys.excepthook
sys.excepthook = new_hook |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _summarize_action(self, root_action):
"""Return a dictionary with various information about this root_action. Note: Scoring assumes that each actor makes the "best" choices in their turn based on the simulation available. """ |
def is_target_node(node):
return isinstance(node, base.EOT) or (node is root_action)
# store per-turn results from the bottom up.
realistic_ends_by_node = dict()
for node in root_action.post_order_nodes(): # bottom up to this action
# only work with EOT or the root action
if not is_target_node(node):
continue
# by the time this node is reached, the results of all children
# have been stored under it in realistic_ends (except leaves)
# so choose the best of the available results and send it
# up to the next valid node
try: # get the results stored previously in the deeper turn
realistic_ends = realistic_ends_by_node[node]
except KeyError: # leaves are own realistic end
realistic_ends = [node]
# identify the "best" end for this node
if node is root_action:
active = node.parent.active
passive = node.parent.passive
else:
active = node.parent.passive
passive = node.parent.active
ends_by_score = dict()
for realistic_end in realistic_ends:
# determine the relative score. i.e. if delta is positive
# then the end result is better for active than passive
relative_score = self._relative_score(node, realistic_end,
active, passive)
ends_by_score[relative_score] = realistic_end
best_end = ends_by_score[max(ends_by_score.keys())]
# done after determining realistic result for root action
if node is root_action:
return self._summarize_result(root_action, best_end)
# not done: place best end on the parent EOT's list of possibilities
parent = node.parent
while parent:
if is_target_node(parent):
break
parent = parent.parent # keep moving up until target found
# at this point the parent is either root_action or another EOT
realistic_ends_by_node.setdefault(parent, list()).append(best_end)
pass |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _relative_score(self, start_eot, end_eot, active, passive):
"""Return the balance of perception between the two nodes. A positive score indicates the result is relatively better for active. """ |
active_start = self._score_eot_for_actor(start_eot, active)
passive_start = self._score_eot_for_actor(start_eot, passive)
active_end = self._score_eot_for_actor(end_eot, active)
passive_end = self._score_eot_for_actor(end_eot, passive)
return (active_end - passive_end) - (active_start - passive_start) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _score_eot_for_actor(self, eot, actor):
"""Have the actor evaluate the end of turn for itself only.""" |
# currently just simple sum of own attributes
# could be much more sophisticated in both analysis (e.g. formulas)
# and breadth of items analyzed (e.g. require other actor, the board)
end_state = eot.parent
a = {'player': end_state.player,
'opponent': end_state.opponent}[actor.name]
# simple prioritization without regard to character attributes
health = a.health * 2
r, g, b, y = a.r, a.g, a.b, a.y
x, m = 0.5 * a.x, 0.5 * a.m
return sum((health, r, g, b, y, x, m)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _summarize_result(self, root_action, leaf_eot):
"""Return a dict with useful information that summarizes this action.""" |
root_board = root_action.parent.board
action_detail = root_action.position_pair
score = self._relative_score(root_action, leaf_eot,
root_action.parent.player,
root_action.parent.opponent)
# mana drain info
total_leaves = 0
mana_drain_leaves = 0
for leaf in root_action.leaves():
total_leaves += 1
if leaf.is_mana_drain:
mana_drain_leaves += 1
summary = base.Summary(root_board, action_detail, score,
mana_drain_leaves, total_leaves)
return summary |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def export_phase_error_hdf5(h5path, identifier, index, phase, mphase, model, n0, r0, spi_params):
"""Export the phase error to an hdf5 file Parameters h5path: str or pathlib.Path path to hdf5 output file identifier: str unique identifier of the input phase (e.g. `qpimage.QPImage["identifier"]`) index: int iteration index phase: 2d real-valued np.ndarray phase image mphase: 2d real-valued np.ndarray reference phase image model: str sphere model name n0: float initial object index r0: float initial object radius [m] spi_params: dict parameter dictionary of :func:`SpherePhaseInterpolator` """ |
with h5py.File(h5path, mode="a") as h5:
if identifier in h5:
grp = h5[identifier]
else:
grp = h5.create_group(identifier)
ds = grp.create_dataset("phase_error_{:05d}".format(index),
data=mphase-phase)
ds.attrs["index initial"] = n0
ds.attrs["radius initial"] = r0
ds.attrs["sim model"] = model
ds.attrs["sim index"] = spi_params["sphere_index"]
ds.attrs["sim radius"] = spi_params["radius"]
ds.attrs["sim center"] = spi_params["center"]
ds.attrs["fit iteration"] = index
ds.attrs["fit iteration"] = index |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def scripts(cls, pkg, metadata, paths=[], **kwargs):
"""This class method is the preferred way to create SceneScript objects. :param str pkg: The dotted name of the package containing the scripts. :param metadata: A mapping or data object. This parameter permits searching among scripts against particular criteria. Its use is application specific. :param list(str) paths: A sequence of file paths to the scripts relative to the package. You can satisfy all parameter requirements by passing in a :py:class:`~turberfield.dialogue.model.SceneScript.Folder` object like this:: SceneScript.scripts(**folder._asdict()) The method generates a sequence of :py:class:`~turberfield.dialogue.model.SceneScript` objects. """ |
for path in paths:
try:
fP = pkg_resources.resource_filename(pkg, path)
except ImportError:
cls.log.warning(
"No package called {}".format(pkg)
)
else:
if not os.path.isfile(fP):
cls.log.warning(
"No script file at {}".format(os.path.join(*pkg.split(".") + [path]))
)
else:
yield cls(fP, metadata) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def read(text, name=None):
"""Read a block of text as a docutils document. :param str text: Scene script text. :param str name: An optional name for the document. :return: A document object. """ |
doc = docutils.utils.new_document(name, SceneScript.settings)
parser = docutils.parsers.rst.Parser()
parser.parse(text, doc)
return doc |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def select(self, personae, relative=False, roles=1):
"""Select a persona for each entity declared in the scene. :param personae: A sequence of Personae. :param bool relative: Affects imports from namespace packages. Used for testing only. :param int roles: The maximum number of roles allocated to each persona. :return: An OrderedDict of {Entity: Persona}. """ |
def constrained(entity):
return (
len(entity["options"].get("types", [])) +
len(entity["options"].get("states", []))
)
rv = OrderedDict()
performing = defaultdict(set)
pool = list(personae)
self.log.debug(pool)
entities = OrderedDict([
("".join(entity.attributes["names"]), entity)
for entity in sorted(
group_by_type(self.doc)[EntityDirective.Declaration],
key=constrained,
reverse=True
)
])
for e in entities.values():
types = tuple(filter(
None,
(e.string_import(t, relative)
for t in e["options"].get("types", []))
))
states = tuple(filter(
None,
(int(t) if t.isdigit() else e.string_import(t, relative)
for t in e["options"].get("states", []))
))
otherRoles = {i.lower() for i in e["options"].get("roles", [])}
typ = types or object
persona = next(
(i for i in pool
if isinstance(i, typ) and
getattr(i, "get_state", not states) and
all(str(i.get_state(type(s))).startswith(str(s)) for s in states) and
(performing[i].issubset(otherRoles) or not otherRoles)),
None
)
rv[e] = persona
performing[persona].update(set(e.attributes["names"]))
if not otherRoles or list(rv.values()).count(persona) == roles:
try:
pool.remove(persona)
except ValueError:
self.log.debug(
"No persona for type {0} and states {1} with {2} {3}.".format(
typ, states, roles, "role" if roles == 1 else "roles"
)
)
return rv |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def cast(self, mapping):
"""Allocate the scene script a cast of personae for each of its entities. :param mapping: A dictionary of {Entity, Persona} :return: The SceneScript object. """ |
# See 'citation' method in
# http://docutils.sourceforge.net/docutils/parsers/rst/states.py
for c, p in mapping.items():
self.doc.note_citation(c)
self.doc.note_explicit_target(c, c)
c.persona = p
self.log.debug("{0} to be played by {1}".format(
c["names"][0].capitalize(), p)
)
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def run(self):
"""Parse the script file. :rtype: :py:class:`~turberfield.dialogue.model.Model` """ |
model = Model(self.fP, self.doc)
self.doc.walkabout(model)
return model |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def my_main(context):
""" The starting point for your app.""" |
print('starting MyApp...')
if context['debug']:
print('Context:')
for k in context:
print('Key: {}\nValue: {}'.format(k, context[k]))
print('Done!')
return 0 |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def versus_summaries(turns=2, sims_to_average=2, async_results_q=None):
"""Return summaries of the likely resutls of each available action.. Arguments: - turns: how many turns to simulate. - in 2013, 1 is fast (seconds), 2 is slow (seconds), 3 who knows - sims_to_average: how many times to run the simulation to get more representative average results of each action. - async_results_q: provide a multiprocessing Queue on which the summaries of each turn will be placed. this is an asynchronous alternative to waiting for the final return value """ |
board, player, opponent, extra_actions = _state_investigator.get_versus()
if extra_actions: extra_actions = 1 # limit value for realistic time
if board is None:
return tuple()
averaged_summaries = list() # default return value is empty
# keep a separate advisor for each simulation to average
advisors = list()
for i in range(sims_to_average):
advisor = versus.Advisor()
advisor.reset(board, player, opponent, extra_actions)
advisors.append(advisor)
# provide async sim results per turn; final results as return value
for turn in range(turns):
# store {action: list of results from each simulation}
summaries_by_action = dict()
for i in range(sims_to_average):
advisor = advisors[i]
advisor.simulate_next_turn()
for s in advisor.sorted_current_summaries():
summaries_by_action.setdefault(s.action, list()).append(s)
# now all sims and analysis for this turn have been completed
averaged_summaries = list()
for action, summaries in summaries_by_action.items():
board = summaries[0].board # any board. they are all the same
action = summaries[0].action # any action. they are all the same
score_sum = sum(s.score for s in summaries)
score_avg = score_sum / len(summaries)
manadrain_sum = sum(s.mana_drain_leaves for s in summaries)
leaves_sum = sum(s.total_leaves for s in summaries)
avg_summary = base.Summary(board, action, score_avg,
manadrain_sum, leaves_sum)
averaged_summaries.append(avg_summary)
averaged_summaries.sort(key=lambda s: s.score, reverse=True)
# option to provide the results asynchronouslys
if not async_results_q is None:
async_results_q.put(averaged_summaries)
return averaged_summaries |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def build_tree_pathname(filename, directory_depth=8, pathname_separator_character=os.sep):
""" Return a pathname built of the specified number of sub-directories, and where each directory is named after the nth letter of the filename corresponding to the directory depth. Examples:: 'f/o/' '0/1/2/3/4/5/6/7/' @param filename: name of a file, with or without extension. @param directory_depth: number of sub-directories to be generated. @param pathname_separator_character: character to be used to separate pathname components, such as '/' for POSIX and '\\' for Windows. If not defined, the default is the character used by the operating system ``os.sep``. @return: a file pathname. """ |
pathname = ''
(filename_without_extension, _file_extension_) = os.path.splitext(filename)
for i in range(min(directory_depth, len(filename_without_extension))):
pathname += filename_without_extension[i] + pathname_separator_character
return pathname |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def build_tree_file_pathname(filename, directory_depth=8, pathname_separator_character=os.sep):
""" Return a file pathname which pathname is built of the specified number of sub-directories, and where each directory is named after the nth letter of the filename corresponding to the directory depth. Examples:: 'f/o/foo.txt' '0/1/2/3/4/5/6/7/0123456789abcdef' @param filename: name of a file, with or without extension. @param directory_depth: number of sub-directories to be generated. @param pathname_separator_character: character to be used to separate pathname components, such as '/' for POSIX and '\\' for Windows. If not defined, the default is the character used by the operating system ``os.sep``. @return: a file pathname. """ |
return build_tree_pathname(filename, directory_depth, pathname_separator_character) + filename |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def find_root_path(absolute_path, relative_path):
""" Return the root path of a path relative to an absolute path. Example: @param absolute_path: an absolute path that is ended by the specified relative path. @param relative_path: a relative path that ends the specified absolute path. @return: the root path of the relative path. """ |
_absolute_path = os.path.normpath(absolute_path)
_relative_path = os.path.normpath(relative_path)
index = _absolute_path.rfind(_relative_path)
if index == -1 or len(_relative_path) + index < len(_absolute_path):
raise ValueError('The relative path does not end the specified absolute path')
return _absolute_path[:index] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_file_checksum(file_path_name, hash_algorithm_name='md5'):
""" Generate the MD5 checksum of the specified file. @param file_path_name: the absolute path and name of the file to generate its MD5 checksum. @param hash_algorithm_name: specify the hash algorithm to use. Refer to ``hashlib.algorithms`` to get the names of the hash algorithms guaranteed to be supported by this module. @return: hash digest returned as a string of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments. @note: the file can not be entirely read to memory, but it needs to be read by chunks of memory that will be freed after each iteration. What's important to notice is that the file must be opened in binary mode. The function breaks the file into chunks using block size of any multiple of 128 (say 8192, 32768, etc.) and its feed them to MD5 consecutively using ``update()``. This takes advantage advantage of the fact that MD5 has 128-byte digest blocks. The function actually uses a block size that depends on the block size of the filesystem to avoid performances issues. @note: The ``F_FRSIZE`` value is the actual minimum allocation unit of the filesystem, while the ``F_BSIZE`` is the block size that would lead to most efficient use of the disk with io calls. """ |
hash_algorithm = hashlib.new(hash_algorithm_name)
if sys.platform == "win32":
import ctypes
sectors_per_cluster = ctypes.c_ulonglong(0)
bytes_per_sector = ctypes.c_ulonglong(0)
root_path_name = ctypes.c_wchar_p(u"C:\\")
ctypes.windll.kernel32.GetDiskFreeSpaceW(root_path_name,
ctypes.pointer(sectors_per_cluster),
ctypes.pointer(bytes_per_sector),
None,
None)
block_size = bytes_per_sector.value
else:
import statvfs
block_size = os.statvfs('/')[statvfs.F_BSIZE]
with open(file_path_name, 'rb') as handle:
for chunk in iter(lambda: handle.read(block_size), b''):
hash_algorithm.update(chunk)
return hash_algorithm.hexdigest() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def make_directory_if_not_exists(path):
""" Create the specified path, making all intermediate-level directories needed to contain the leaf directory. Ignore any error that would occur if the leaf directory already exists. @note: all the intermediate-level directories are created with the default mode is 0777 (octal). @param path: the path to create. @raise OSError: an error that would occur if the path cannot be created. """ |
try:
os.makedirs(path)
except OSError, error: # Ignore if the directory has been already created.
if error.errno <> errno.EEXIST:
raise error |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def move_file(source_file_pathname, destination_file_pathname):
""" Move the the specified file to another location. If the destination already exists, it is replaced silently. This function is an alternative to ``shutil.move(src, dst)``, which might raise ``OSError`` if the destination already exists. @param source_file_pathname: the complete path and name of the file to move. @param destination_file_pathname: the complete path and name of the file once moved. """ |
if os.path.exists(destination_file_pathname):
os.remove(destination_file_pathname)
shutil.move(source_file_pathname, destination_file_pathname) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def which(exe_name):
""" Locate a program file in the user's path. @param exec_name: name of the executable file. @return: ``None`` if the executable has not been found in the user's path, or the path for the executable file. """ |
def is_exe(file_path_name):
return os.path.isfile(file_path_name) and os.access(file_path_name, os.X_OK)
is_platform_windows = (platform.system() == 'Windows')
fpath, _fname = os.path.split(exe_name)
if fpath:
if is_exe(exe_name):
return exe_name
else:
for path in os.environ['PATH'].split(os.pathsep):
exe_file_path_name = os.path.join(path, exe_name)
if is_exe(exe_file_path_name):
return exe_file_path_name
if is_platform_windows:
windows_exe_file_path_name = '%s.exe' % exe_file_path_name
if is_exe(windows_exe_file_path_name):
return windows_exe_file_path_name
windows_com_file_path_name = '%s.exe' % exe_file_path_name
if is_exe(windows_com_file_path_name):
return windows_com_file_path_name
return None |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_time(filename, tags):
"""
Get most reliable timestamp from a picture, running down a couple of options.
Filename, exif tags, modification time.
""" |
# use exif 'Image DateTime' field as the time
if 'Image DateTime' in tags.keys():
return time.strptime(str(tags['Image DateTime']), '%Y:%m:%d %H:%M:%S')
# very fuzzy time machting on filename
# TODO: very fuzzy part, now it just matches the iphone naming convention
m = re.match('^(\d{4}-\d{2}-\d{2} \d{2}\.\d{2}\.\d{2}).*', filename)
if m:
return time.strptime(m.group(0), '%Y-%m-%d %H.%M.%S')
# if all else fails use stat().st_mtime (consistent on windows/linux as last modification time)
return time.localtime(Path(filename).stat().st_mtime) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def location_filter(files_with_tags, location, radius):
'''
Get photos taken within the specified radius from a given point.
'''
on_location = dict()
for f, tags in files_with_tags.items():
if 'GPS GPSLatitude' in tags:
try:
lat = convert_to_decimal(str(tags['GPS GPSLatitude']))
long = convert_to_decimal(str(tags['GPS GPSLongitude']))
except ValueError:
print('{0} has invalid gps info'.format(f))
try:
if haversine(lat, long, location['lat'], location['long']) < radius:
on_location[f] = tags
except InvalidCoordinate:
print('{0} has invalid gps info'.format(f))
return on_location |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def add_based_on_time(files_with_tags, on_location):
'''
Sometimes the first photo in a series does not have gps coordinates because the phone
doesnt have a gps-fix yet. To add these photos as well we take the list of photos wich where
taken in the right location. Then add any photos taken whitin 10 minutes of these photos,
because they are almost certainly taken in the same area.
'''
to_add = dict()
for veiling_f, veiling_tags in on_location.items():
for compare_f, compare_tags in files_with_tags.items():
delta = abs(veiling_tags['TIME'] - compare_tags['TIME'])
if (delta.total_seconds() < 10 * 60) and (compare_f not in on_location.keys()):
to_add[compare_f] = compare_tags
return to_add |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _debug(self, msg, *args, **kwargs):
"""Emit debugging messages.""" |
# Do nothing if debugging is disabled
if self._debug_stream is None or self._debug_stream is False:
return
# What are we passing to the format?
if kwargs:
fmtargs = kwargs
else:
fmtargs = args
# Emit the message
print >>self._debug_stream, msg % fmtargs |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _make_req(self, method, url, methname, headers=None):
"""Create a request object for the specified method and url.""" |
# Build up headers
hset = hdrs.HeaderDict()
# Walk through our global headers
for hdr, value in self._headers.items():
# If it's a callable, call it
if callable(value):
value = value(methname)
else:
# OK, just stringify it
value = str(value)
# If it's meaningful, attach it
if value:
hset[hdr] = value
# Were headers passed in?
if headers is not None:
# Update from specified headers
hset.update(headers)
# Hook method to instantiate requests
self._debug("Creating request %s.%s(%r, %r, headers=%r)",
self._req_class.__module__, self._req_class.__name__,
method, url, hset)
return self._req_class(method, url, self._client, self._procstack,
headers=hset, debug=self._debug) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def query_plugins(entry_point_name):
""" Find everything with a specific entry_point. Results will be returned as a dictionary, with the name as the key and the entry_point itself as the value. Arguments: entry_point_name - the name of the entry_point to populate """ |
entries = {}
for entry_point in pkg_resources.iter_entry_points(entry_point_name):
entries[entry_point.name] = entry_point.load()
return entries |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def prefixedDec(nstr, schema):
""" !~~prefixedDec corresponding strings in documents must begin with the associated string in the schema, and the right part of strings in documents must be decimal. """ |
if not nstr.startswith(schema):
return False
postfix = nstr[len(schema):]
try:
int(postfix)
except ValueError:
return False
return True |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _transschema(x):
""" Transform a schema, once loaded from its YAML representation, to its final internal representation """ |
if isinstance(x, tuple):
return x.__class__(_transschema(x[0]), *x[1:])
elif isinstance(x, dict):
return dict((_qualify_map(key, _transschema(val)) for key, val in x.iteritems()))
elif isinstance(x, list):
return map(_transschema, x)
else:
return x |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _fill_entries(self):
"""Populate entries dictionaries from index.""" |
for file in self.index.files:
path = self.index.paths[file.pathIndex]
data_offset = file.dataOffset
data_size = file.dataSize
obj = RAFEntry(self._data_handle, path, data_offset, data_size)
# Add to full path dictionary
assert path not in self.entries_full
self.entries_full[path.lower()] = obj
# Add to name dictionary
name = os.path.basename(path).lower()
if name not in self.entries_name:
self.entries_name[name] = []
self.entries_name[name].append(obj) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def find(self, path=None, name=None):
""" Returns most recent version of the matching file. If there are multiple files of the same name and version, a random one is used. """ |
if path is None and name is None:
# TODO: Correct error type
raise ValueError("Path or name is required.")
# Entries are sorted by version number, so the last will be the most recent
# TODO: Reduce redundancy with RAFArchive
if path:
return self.entries_full[path.lower()][-1]
else:
return self.entries_name[name.lower()][-1][-1] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def find_re(self, pattern):
"""Find the most recent versions of all entries whose path matches a given pattern.""" |
# TODO: Reduce redundancy with RAFArchive
pattern = re.compile(pattern, re.I)
for k, v in six.iteritems(self.entries_full):
if pattern.search(k):
# Most recent version will be last
yield v[-1] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def colorize(string, color, *args, **kwargs):
""" Implements string formatting along with color specified in colorama.Fore """ |
string = string.format(*args, **kwargs)
return color + string + colorama.Fore.RESET |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def legitimize(text):
"""Converts a string to a valid filename. """ |
import platform
os_ = platform.system()
# POSIX systems
text = text.translate({
0: None,
ord('/'): '-',
ord('|'): '-',
})
if os_ == 'Windows':
# Windows (non-POSIX namespace)
text = text.translate({
# Reserved in Windows VFAT and NTFS
ord(':'): '-',
ord('*'): '-',
ord('?'): '-',
ord('\\'): '-',
ord('\"'): '\'',
# Reserved in Windows VFAT
ord('+'): '-',
ord('<'): '-',
ord('>'): '-',
ord('['): '(',
ord(']'): ')',
})
else:
# *nix
if os_ == 'Darwin':
# Mac OS HFS+
text = text.translate({
ord(':'): '-',
})
# Remove leading .
if text.startswith("."):
text = text[1:]
text = text[:82] # Trim to 82 Unicode characters long
return text |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.