docstring stringlengths 52 499 | function stringlengths 67 35.2k | __index_level_0__ int64 52.6k 1.16M |
|---|---|---|
Authenticate to Google with the provided credentials.
Args:
email (str): The account to use.
password (str): The account password.
android_id (str): An identifier for this client.
Raises:
LoginException: If there was a problem logging in. | def login(self, email, password, android_id):
self._email = email
self._android_id = android_id
res = gpsoauth.perform_master_login(self._email, password, self._android_id)
if 'Token' not in res:
raise exception.LoginException(res.get('Error'), res.get('ErrorDetail')... | 323,046 |
Authenticate to Google with the provided master token.
Args:
email (str): The account to use.
master_token (str): The master token.
android_id (str): An identifier for this client.
Raises:
LoginException: If there was a problem logging in. | def load(self, email, master_token, android_id):
self._email = email
self._android_id = android_id
self._master_token = master_token
self.refresh()
return True | 323,047 |
Send an authenticated request to a Google API.
Automatically retries if the access token has expired.
Args:
**req_kwargs: Arbitrary keyword arguments to pass to Requests.
Return:
dict: The parsed JSON response.
Raises:
APIException: If the server re... | def send(self, **req_kwargs):
i = 0
while True:
response = self._send(**req_kwargs).json()
if 'error' not in response:
break
error = response['error']
if error['code'] != 401:
raise exception.APIException(error['co... | 323,051 |
Send an authenticated request to a Google API.
Args:
**req_kwargs: Arbitrary keyword arguments to pass to Requests.
Return:
requests.Response: The raw response.
Raises:
LoginException: If :py:meth:`login` has not been called. | def _send(self, **req_kwargs):
auth_token = self._auth.getAuthToken()
if auth_token is None:
raise exception.LoginException('Not logged in')
req_kwargs.setdefault('headers', {
'Authorization': 'OAuth ' + auth_token
})
return self._session.reques... | 323,052 |
Sync up (and down) all changes.
Args:
target_version (str): The local change version.
nodes (List[dict]): A list of nodes to sync up to the server.
labels (List[dict]): A list of labels to sync up to the server.
Return:
dict: Description of all changes.
... | def changes(self, target_version=None, nodes=None, labels=None):
if nodes is None:
nodes = []
if labels is None:
labels = []
current_time = time.time()
params = {
'nodes': nodes,
'clientTimestamp': _node.NodeTimestamps.int_to_str... | 323,054 |
Get the canonical link to a media blob.
Args:
blob (gkeepapi.node.Blob): The blob.
Returns:
str: A link to the media. | def get(self, blob):
return self._send(
url=self._base_url + blob.parent.server_id + '/' + blob.server_id + '?s=0',
method='GET',
allow_redirects=False
).headers.get('Location') | 323,056 |
Authenticate to Google with the provided credentials & sync.
Args:
email (str): The account to use.
password (str): The account password.
state (dict): Serialized state to load.
Raises:
LoginException: If there was a problem logging in. | def login(self, username, password, state=None, sync=True):
auth = APIAuth(self.OAUTH_SCOPES)
ret = auth.login(username, password, get_mac())
if ret:
self.load(auth, state, sync)
return ret | 323,064 |
Authenticate to Google with the provided master token & sync.
Args:
email (str): The account to use.
master_token (str): The master token.
state (dict): Serialized state to load.
Raises:
LoginException: If there was a problem logging in. | def resume(self, email, master_token, state=None, sync=True):
auth = APIAuth(self.OAUTH_SCOPES)
ret = auth.load(email, master_token, android_id=get_mac())
if ret:
self.load(auth, state, sync)
return ret | 323,065 |
Authenticate to Google with a prepared authentication object & sync.
Args:
auth (APIAuth): Authentication object.
state (dict): Serialized state to load.
Raises:
LoginException: If there was a problem logging in. | def load(self, auth, state=None, sync=True):
self._keep_api.setAuth(auth)
self._reminders_api.setAuth(auth)
self._media_api.setAuth(auth)
if state is not None:
self.restore(state)
if sync:
self.sync(True) | 323,066 |
Serialize note data.
Args:
state (dict): Serialized state to load. | def dump(self):
# Find all nodes manually, as the Keep object isn't aware of new ListItems
# until they've been synced to the server.
nodes = []
for node in self.all():
nodes.append(node)
for child in node.children:
nodes.append(child)
... | 323,067 |
Unserialize saved note data.
Args:
state (dict): Serialized state to load. | def restore(self, state):
self._clear()
self._parseUserInfo({'labels': state['labels']})
self._parseNodes(state['nodes'])
self._keep_version = state['keep_version'] | 323,068 |
Get a note with the given ID.
Args:
node_id (str): The note ID.
Returns:
gkeepapi.node.TopLevelNode: The Note or None if not found. | def get(self, node_id):
return \
self._nodes[_node.Root.ID].get(node_id) or \
self._nodes[_node.Root.ID].get(self._sid_map.get(node_id)) | 323,069 |
Register a top level node (and its children) for syncing up to the server. There's no need to call this for nodes created by
:py:meth:`createNote` or :py:meth:`createList` as they are automatically added.
LoginException: If :py:meth:`login` has not been called.
Args:
node (gkeep... | def add(self, node):
if node.parent_id != _node.Root.ID:
raise exception.InvalidException('Not a top level node')
self._nodes[node.id] = node
self._nodes[node.parent_id].append(node, False) | 323,070 |
Create a new managed note. Any changes to the note will be uploaded when :py:meth:`sync` is called.
Args:
title (str): The title of the note.
text (str): The text of the note.
Returns:
gkeepapi.node.List: The new note. | def createNote(self, title=None, text=None):
node = _node.Note()
if title is not None:
node.title = title
if text is not None:
node.text = text
self.add(node)
return node | 323,072 |
Create a new list and populate it. Any changes to the note will be uploaded when :py:meth:`sync` is called.
Args:
title (str): The title of the list.
items (List[(str, bool)]): A list of tuples. Each tuple represents the text and checked status of the listitem.
Returns:
... | def createList(self, title=None, items=None):
if items is None:
items = []
node = _node.List()
if title is not None:
node.title = title
for text, checked in items:
node.add(text, checked)
self.add(node)
return node | 323,073 |
Create a new label.
Args:
name (str): Label name.
Returns:
gkeepapi.node.Label: The new label.
Raises:
LabelException: If the label exists. | def createLabel(self, name):
if self.findLabel(name):
raise exception.LabelException('Label exists')
node = _node.Label()
node.name = name
self._labels[node.id] = node # pylint: disable=protected-access
return node | 323,074 |
Find a label with the given name.
Args:
name (Union[_sre.SRE_Pattern, str]): A str or regular expression to match against the name.
create (bool): Whether to create the label if it doesn't exist (only if name is a str).
Returns:
Union[gkeepapi.node.Label, None]: The... | def findLabel(self, query, create=False):
if isinstance(query, six.string_types):
query = query.lower()
for label in self._labels.values():
if (isinstance(query, six.string_types) and query == label.name.lower()) or \
(isinstance(query, Pattern) and quer... | 323,075 |
Deletes a label.
Args:
label_id (str): Label id. | def deleteLabel(self, label_id):
if label_id not in self._labels:
return
label = self._labels[label_id]
label.delete()
for node in self.all():
node.labels.remove(label) | 323,076 |
Sync the local Keep tree with the server. If resyncing, local changes will be detroyed. Otherwise, local changes to notes, labels and reminders will be detected and synced up.
Args:
resync (bool): Whether to resync data.
Raises:
SyncException: If there is a consistency issue. | def sync(self, resync=False):
if resync:
self._clear()
while True:
logger.debug('Starting reminder sync: %s', self._reminder_version)
changes = self._reminders_api.list()
if 'task' in changes:
self._parseTasks(changes['task'])
... | 323,077 |
Helper to construct a node from a dict.
Args:
raw (dict): Raw node representation.
Returns:
Node: A Node object or None. | def from_json(raw):
ncls = None
_type = raw.get('type')
try:
ncls = _type_map[NodeType(_type)]
except (KeyError, ValueError) as e:
logger.warning('Unknown node type: %s', _type)
if DEBUG:
raise_from(exception.ParseException('Parse error for %s' % (_type), raw), e... | 323,083 |
Unserialize from raw representation. (Wrapper)
Args:
raw (dict): Raw.
Raises:
ParseException: If there was an error parsing data. | def load(self, raw):
try:
self._load(raw)
except (KeyError, ValueError) as e:
raise_from(exception.ParseException('Parse error in %s' % (type(self)), raw), e) | 323,085 |
Serialize into raw representation. Clears the dirty bit by default.
Args:
clean (bool): Whether to clear the dirty bit.
Returns:
dict: Raw. | def save(self, clean=True):
ret = {}
if clean:
self._dirty = False
else:
ret['_dirty'] = self._dirty
return ret | 323,086 |
Helper to construct an annotation from a dict.
Args:
raw (dict): Raw annotation representation.
Returns:
Node: An Annotation object or None. | def from_json(cls, raw):
bcls = None
if 'webLink' in raw:
bcls = WebLink
elif 'topicCategory' in raw:
bcls = Category
elif 'taskAssist' in raw:
bcls = TaskAssist
elif 'context' in raw:
bcls = Context
if bcls is Non... | 323,101 |
Add an annotation.
Args:
annotation (gkeepapi.node.Annotation): An Annotation object.
Returns:
gkeepapi.node.Annotation: The Annotation. | def append(self, annotation):
self._annotations[annotation.id] = annotation
self._dirty = True
return annotation | 323,107 |
Removes an annotation.
Args:
annotation (gkeepapi.node.Annotation): An Annotation object.
Returns:
gkeepapi.node.Annotation: The Annotation. | def remove(self, annotation):
if annotation.id in self._annotations:
del self._annotations[annotation.id]
self._dirty = True | 323,108 |
Add a collaborator.
Args:
str : Collaborator email address. | def add(self, email):
if email not in self._collaborators:
self._collaborators[email] = ShareRequestValue.Add
self._dirty = True | 323,118 |
Remove a Collaborator.
Args:
str : Collaborator email address. | def remove(self, email):
if email in self._collaborators:
if self._collaborators[email] == ShareRequestValue.Add:
del self._collaborators[email]
else:
self._collaborators[email] = ShareRequestValue.Remove
self._dirty = True | 323,119 |
Add a label.
Args:
label (gkeepapi.node.Label): The Label object. | def add(self, label):
self._labels[label.id] = label
self._dirty = True | 323,123 |
Remove a label.
Args:
label (gkeepapi.node.Label): The Label object. | def remove(self, label):
if label.id in self._labels:
self._labels[label.id] = None
self._dirty = True | 323,124 |
Mark the node as dirty.
Args:
edited (bool): Whether to set the edited time. | def touch(self, edited=False):
self._dirty = True
dt = datetime.datetime.utcnow()
self.timestamps.updated = dt
if edited:
self.timestamps.edited = dt | 323,125 |
Set the text value.
Args:
value (str): Text value. | def text(self, value):
self._text = value
self.timestamps.edited = datetime.datetime.utcnow()
self.touch(True) | 323,131 |
Add a new child node.
Args:
node (gkeepapi.Node): Node to add.
dirty (bool): Whether this node should be marked dirty. | def append(self, node, dirty=True):
self._children[node.id] = node
node.parent = self
if dirty:
self.touch()
return node | 323,133 |
Remove the given child node.
Args:
node (gkeepapi.Node): Node to remove.
dirty (bool): Whether this node should be marked dirty. | def remove(self, node, dirty=True):
if node.id in self._children:
self._children[node.id].parent = None
del self._children[node.id]
if dirty:
self.touch() | 323,134 |
Add a new item to the list.
Args:
text (str): The text.
checked (bool): Whether this item is checked.
sort (int): Item id for sorting. | def add(self, text, checked=False, sort=None):
node = ListItem(parent_id=self.id, parent_server_id=self.server_id)
node.checked = checked
node.text = text
if sort is not None:
node.sort = sort
self.append(node, True)
self.touch(True)
return no... | 323,145 |
Sort list items, taking into account parent items.
Args:
items (list[gkeepapi.node.ListItem]): Items to sort.
Returns:
list[gkeepapi.node.ListItem]: Sorted items. | def items_sort(cls, items):
class t(tuple):
def __cmp__(self, other):
for a, b in six.moves.zip_longest(self, other):
if a != b:
if a is None:
return 1
if b is None:
... | 323,146 |
Add a new sub item to the list. This item must already be attached to a list.
Args:
text (str): The text.
checked (bool): Whether this item is checked.
sort (int): Item id for sorting. | def add(self, text, checked=False, sort=None):
if self.parent is None:
raise exception.InvalidException('Item has no parent')
node = self.parent.add(text, checked, sort)
self.indent(node)
return node | 323,152 |
Indent an item. Does nothing if the target has subitems.
Args:
node (gkeepapi.node.ListItem): Item to indent.
dirty (bool): Whether this node should be marked dirty. | def indent(self, node, dirty=True):
if node.subitems:
return
self._subitems[node.id] = node
node.super_list_item_id = self.id
node.parent_item = self
if dirty:
node.touch(True) | 323,153 |
Dedent an item. Does nothing if the target is not indented under this item.
Args:
node (gkeepapi.node.ListItem): Item to dedent.
dirty (bool): Whether this node should be marked dirty. | def dedent(self, node, dirty=True):
if node.id not in self._subitems:
return
del self._subitems[node.id]
node.super_list_item_id = None
node.parent_item = None
if dirty:
node.touch(True) | 323,154 |
Helper to construct a blob from a dict.
Args:
raw (dict): Raw blob representation.
Returns:
NodeBlob: A NodeBlob object or None. | def from_json(cls, raw):
if raw is None:
return None
bcls = None
_type = raw.get('type')
try:
bcls = cls._blob_type_map[BlobType(_type)]
except (KeyError, ValueError) as e:
logger.warning('Unknown blob type: %s', _type)
if... | 323,172 |
Form a complex number.
Arguments:
f -- comma delimited file w/ x,y coordinates
xs -- if f not specified this is a file w/ x coordinates
ys -- if f not specified this is a filew / y coordinates
size -- size of the plot
pch -- shape of the points (any character)
colour... | def plot_scatter(f, xs, ys, size, pch, colour, title):
cs = None
if f:
if isinstance(f, str):
with open(f) as fh:
data = [tuple(line.strip().split(',')) for line in fh]
else:
data = [tuple(line.strip().split(',')) for line in f]
xs = [float(i[... | 323,886 |
Computes Levenshtein Distance between two words
Args:
:param w1: str
:param w2: str
:return: int
Examples:
>>> Levenshtein.Levenshtein_Distance('noctis', 'noctem')
2
>>> Levenshtein.Levenshtein_Distance('nox', 'nochem')
... | def Levenshtein_Distance(w1, w2):
m, n = len(w1), len(w2)
v1 = [i for i in range(n + 1)]
v2 = [0 for i in range(n + 1)]
for i in range(m):
v2[0] = i + 1
for j in range(n):
delCost = v1[j + 1] + 1
insCost = v2[j] + 1
... | 323,903 |
Set up a unicode character.
Arguments:
unicodeHexValue -- an integer that should correspond to a
Unicode code point.
block -- the CharacterBlock this character belongs to.
Raises:
ValueError -- if unicodeHexValue is not a valid code p... | def __init__(self, unicodeHexValue, block):
if unicodeHexValue < 0 or unicodeHexValue > 0x10FFFF:
raise ValueError("numeric value outside Unicode range")
self.unicodeHexValue = unicodeHexValue
self.unichr = py23char(self.unicodeHexValue)
self.name = unicoded... | 324,415 |
Initializes the data collection wrapper.
Args:
env: The environment to monitor.
directory: Where to store collected data.
collect_freq: How often to save simulation state, in terms of environment steps.
flush_freq: How frequently to dump data to disk, in terms of... | def __init__(self, env, directory, collect_freq=1, flush_freq=100):
super().__init__(env)
# the base directory for all logging
self.directory = directory
# in-memory cache for simulation states and action info
self.states = []
self.action_infos = [] # stores i... | 324,561 |
Loads a mujoco xml from file.
Args:
fname (str): path to the MJCF xml file. | def __init__(self, fname):
self.file = fname
self.folder = os.path.dirname(fname)
self.tree = ET.parse(fname)
self.root = self.tree.getroot()
self.name = self.root.get("model")
self.worldbody = self.create_default_element("worldbody")
self.actuator = self... | 324,566 |
Default merge method.
Args:
other: another MujocoXML instance
raises XML error if @other is not a MujocoXML instance.
merges <worldbody/>, <actuator/> and <asset/> of @other into @self
merge_body: True if merging child bodies of @other. Defaults to True. | def merge(self, other, merge_body=True):
if not isinstance(other, MujocoXML):
raise XMLError("{} is not a MujocoXML instance.".format(type(other)))
if merge_body:
for body in other.worldbody:
self.worldbody.append(body)
self.merge_asset(other)
... | 324,569 |
Saves the xml to file.
Args:
fname: output file location
pretty: attempts!! to pretty print the output | def save_model(self, fname, pretty=False):
with open(fname, "w") as f:
xml_str = ET.tostring(self.root, encoding="unicode")
if pretty:
# TODO: get a better pretty print library
parsed_xml = xml.dom.minidom.parseString(xml_str)
xml_... | 324,572 |
Initializes the Gym wrapper.
Args:
env (MujocoEnv instance): The environment to wrap.
keys (list of strings): If provided, each observation will
consist of concatenated keys from the wrapped environment's
observation dictionary. Defaults to robot-state an... | def __init__(self, env, keys=None):
self.env = env
if keys is None:
assert self.env.use_object_obs, "Object observations need to be enabled."
keys = ["robot-state", "object-state"]
self.keys = keys
# set up observation and action spaces
flat_ob ... | 324,574 |
Filters keys of interest out and concatenate the information.
Args:
obs_dict: ordered dictionary of observations | def _flatten_obs(self, obs_dict, verbose=False):
ob_lst = []
for key in obs_dict:
if key in self.keys:
if verbose:
print("adding key: {}".format(key))
ob_lst.append(obs_dict[key])
return np.concatenate(ob_lst) | 324,575 |
Reward function for the task.
The dense reward has three components.
Reaching: in [0, 1], to encourage the arm to reach the cube
Grasping: in {0, 0.25}, non-zero if arm is grasping the cube
Lifting: in {0, 1}, non-zero if arm has lifted the cube
The sparse reward o... | def reward(self, action=None):
reward = 0.
# sparse completion reward
if self._check_success():
reward = 1.0
# use a shaping reward
if self.reward_shaping:
# reaching reward
cube_pos = self.sim.data.body_xpos[self.cube_body_id]
... | 324,596 |
Use the device (keyboard or SpaceNav 3D mouse) to collect a demonstration.
The rollout trajectory is saved to files in npz format.
Modify the DataCollectionWrapper wrapper to add new fields or change data formats.
Args:
env: environment to control
device (instance of Device class): to recei... | def collect_human_trajectory(env, device):
obs = env.reset()
# rotate the gripper so we can see it easily
env.set_robot_joint_positions([0, -1.18, 0.00, 2.18, 0.00, 0.57, 1.5708])
env.viewer.set_camera(camera_id=2)
env.render()
is_first = True
# episode terminates on a spacenav res... | 324,599 |
Initialize a SpaceMouse handler.
Args:
vendor_id: HID device vendor id
product_id: HID device product id
Note:
Use hid.enumerate() to view all USB human interface devices (HID).
Make sure SpaceMouse is detected before running the script.
You ... | def __init__(self, vendor_id=9583, product_id=50735):
print("Opening SpaceMouse device")
self.device = hid.device()
self.device.open(vendor_id, product_id) # SpaceMouse
print("Manufacturer: %s" % self.device.get_manufacturer_string())
print("Product: %s" % self.device... | 324,626 |
Mounts gripper to arm.
Throws error if robot already has a gripper or gripper type is incorrect.
Args:
arm_name (str): name of arm mount
gripper (MujocoGripper instance): gripper MJCF model | def add_gripper(self, arm_name, gripper):
if arm_name in self.grippers:
raise ValueError("Attempts to add multiple grippers to one body")
arm_subtree = self.worldbody.find(".//body[@name='{}']".format(arm_name))
for actuator in gripper.actuator:
if actuator.ge... | 324,629 |
Genreator for grippers
Creates a Gripper instance with the provided name.
Args:
name: the name of the gripper class
Returns:
gripper: Gripper instance
Raises:
XMLError: [description] | def gripper_factory(name):
if name == "TwoFingerGripper":
return TwoFingerGripper()
if name == "LeftTwoFingerGripper":
return LeftTwoFingerGripper()
if name == "PR2Gripper":
return PR2Gripper()
if name == "RobotiqGripper":
return RobotiqGripper()
if name == "Push... | 324,663 |
Creates an actuator tag with attributes specified by @**kwargs.
Args:
joint: type of actuator transmission.
see all types here: http://mujoco.org/book/modeling.html#actuator
act_type (str): actuator type. Defaults to "actuator" | def new_actuator(joint, act_type="actuator", **kwargs):
element = ET.Element(act_type, attrib=kwargs)
element.set("joint", joint)
return element | 324,676 |
Creates a site element with attributes specified by @**kwargs.
Args:
name (str): site name.
rgba: color and transparency. Defaults to solid red.
pos: 3d position of the site.
size ([float]): site size (sites are spherical by default). | def new_site(name, rgba=RED, pos=(0, 0, 0), size=(0.005,), **kwargs):
kwargs["rgba"] = array_to_string(rgba)
kwargs["pos"] = array_to_string(pos)
kwargs["size"] = array_to_string(size)
kwargs["name"] = name
element = ET.Element("site", attrib=kwargs)
return element | 324,677 |
Creates a geom element with attributes specified by @**kwargs.
Args:
geom_type (str): type of the geom.
see all types here: http://mujoco.org/book/modeling.html#geom
size: geom size parameters.
pos: 3d position of the geom frame.
rgba: color and transparency. Defaults to... | def new_geom(geom_type, size, pos=(0, 0, 0), rgba=RED, group=0, **kwargs):
kwargs["type"] = str(geom_type)
kwargs["size"] = array_to_string(size)
kwargs["rgba"] = array_to_string(rgba)
kwargs["group"] = str(group)
kwargs["pos"] = array_to_string(pos)
element = ET.Element("geom", attrib=kwar... | 324,678 |
Creates a body element with attributes specified by @**kwargs.
Args:
name (str): body name.
pos: 3d position of the body frame. | def new_body(name=None, pos=None, **kwargs):
if name is not None:
kwargs["name"] = name
if pos is not None:
kwargs["pos"] = array_to_string(pos)
element = ET.Element("body", attrib=kwargs)
return element | 324,679 |
Creates a inertial element with attributes specified by @**kwargs.
Args:
mass: The mass of inertial | def new_inertial(name=None, pos=(0, 0, 0), mass=None, **kwargs):
if mass is not None:
kwargs["mass"] = str(mass)
kwargs["pos"] = array_to_string(pos)
element = ET.Element("inertial", attrib=kwargs)
return element | 324,680 |
Force the internal robot model to match the provided joint angles.
Args:
joint_positions (list): a list or flat numpy array of joint positions.
simulate (bool): If True, actually use physics simulation, else
write to physics state directly.
sync_last (bool):... | def sync_ik_robot(self, joint_positions, simulate=False, sync_last=True):
num_joints = len(joint_positions)
if not sync_last:
num_joints -= 1
for i in range(num_joints):
if simulate:
p.setJointMotorControl2(
self.ik_robot,
... | 324,686 |
Convert a pose in the base frame to a pose in the world frame.
Args:
pose_in_base: a (pos, orn) tuple.
Returns:
pose_in world: a (pos, orn) tuple. | def bullet_base_pose_to_world_pose(self, pose_in_base):
pose_in_base = T.pose2mat(pose_in_base)
base_pos_in_world = np.array(p.getBasePositionAndOrientation(self.ik_robot)[0])
base_orn_in_world = np.array(p.getBasePositionAndOrientation(self.ik_robot)[1])
base_pose_in_world = T... | 324,688 |
Converts given rotation matrix to quaternion.
Args:
rmat: 3x3 rotation matrix
precise: If isprecise is True, the input matrix is assumed to be a precise
rotation matrix and a faster algorithm is used.
Returns:
vec4 float quaternion angles | def mat2quat(rmat, precise=False):
M = np.array(rmat, dtype=np.float32, copy=False)[:3, :3]
if precise:
q = np.empty((4,))
t = np.trace(M)
if t > M[3, 3]:
q[0] = t
q[3] = M[1, 0] - M[0, 1]
q[2] = M[0, 2] - M[2, 0]
q[1] = M[2, 1] - M[1,... | 324,703 |
Converts given rotation matrix to euler angles in radian.
Args:
rmat: 3x3 rotation matrix
axes: One of 24 axis sequences as string or encoded tuple
Returns:
converted euler angles in radian vec3 float | def mat2euler(rmat, axes="sxyz"):
try:
firstaxis, parity, repetition, frame = _AXES2TUPLE[axes.lower()]
except (AttributeError, KeyError):
firstaxis, parity, repetition, frame = axes
i = firstaxis
j = _NEXT_AXIS[i + parity]
k = _NEXT_AXIS[i - parity + 1]
M = np.array(rmat,... | 324,704 |
Converts pose to homogeneous matrix.
Args:
pose: a (pos, orn) tuple where pos is vec3 float cartesian, and
orn is vec4 float quaternion.
Returns:
4x4 homogeneous matrix | def pose2mat(pose):
homo_pose_mat = np.zeros((4, 4), dtype=np.float32)
homo_pose_mat[:3, :3] = quat2mat(pose[1])
homo_pose_mat[:3, 3] = np.array(pose[0], dtype=np.float32)
homo_pose_mat[3, 3] = 1.
return homo_pose_mat | 324,705 |
Converts given quaternion (x, y, z, w) to matrix.
Args:
quaternion: vec4 float angles
Returns:
3x3 rotation matrix | def quat2mat(quaternion):
q = np.array(quaternion, dtype=np.float32, copy=True)[[3, 0, 1, 2]]
n = np.dot(q, q)
if n < EPS:
return np.identity(3)
q *= math.sqrt(2.0 / n)
q = np.outer(q, q)
return np.array(
[
[1.0 - q[2, 2] - q[3, 3], q[1, 2] - q[3, 0], q[1, 3] + q... | 324,706 |
Computes the inverse of a homogenous matrix corresponding to the pose of some
frame B in frame A. The inverse is the pose of frame A in frame B.
Args:
pose: numpy array of shape (4,4) for the pose to inverse
Returns:
numpy array of shape (4,4) for the inverse pose | def pose_inv(pose):
# Note, the inverse of a pose matrix is the following
# [R t; 0 1]^-1 = [R.T -R.T*t; 0 1]
# Intuitively, this makes sense.
# The original pose matrix translates by t, then rotates by R.
# We just invert the rotation by applying R-1 = R.T, and also translate back.
# Sin... | 324,707 |
Converts linear and angular velocity of a point in frame A to the equivalent in frame B.
Args:
vel_A: 3-dim iterable for linear velocity in A
ang_vel_A: 3-dim iterable for angular velocity in A
pose_A_in_B: numpy array of shape (4,4) corresponding to the pose of A in frame B
Returns:
... | def vel_in_A_to_vel_in_B(vel_A, ang_vel_A, pose_A_in_B):
pos_A_in_B = pose_A_in_B[:3, 3]
rot_A_in_B = pose_A_in_B[:3, :3]
skew_symm = _skew_symmetric_translation(pos_A_in_B)
vel_B = rot_A_in_B.dot(vel_A) + skew_symm.dot(rot_A_in_B.dot(ang_vel_A))
ang_vel_B = rot_A_in_B.dot(ang_vel_A)
return... | 324,709 |
Converts linear and rotational force at a point in frame A to the equivalent in frame B.
Args:
force_A: 3-dim iterable for linear force in A
torque_A: 3-dim iterable for rotational force (moment) in A
pose_A_in_B: numpy array of shape (4,4) corresponding to the pose of A in frame B
Ret... | def force_in_A_to_force_in_B(force_A, torque_A, pose_A_in_B):
pos_A_in_B = pose_A_in_B[:3, 3]
rot_A_in_B = pose_A_in_B[:3, :3]
skew_symm = _skew_symmetric_translation(pos_A_in_B)
force_B = rot_A_in_B.T.dot(force_A)
torque_B = -rot_A_in_B.T.dot(skew_symm.dot(force_A)) + rot_A_in_B.T.dot(torque_A... | 324,710 |
Makes a homogenous pose matrix from a translation vector and a rotation matrix.
Args:
translation: a 3-dim iterable
rotation: a 3x3 matrix
Returns:
pose: a 4x4 homogenous matrix | def make_pose(translation, rotation):
pose = np.zeros((4, 4))
pose[:3, :3] = rotation
pose[:3, 3] = translation
pose[3, 3] = 1.0
return pose | 324,712 |
Returns the difference between two quaternion orientations as a 3 DOF numpy array.
For use in an impedance controller / task-space PD controller.
Args:
target_orn: 4-dim iterable, desired orientation as a (x, y, z, w) quaternion
current_orn: 4-dim iterable, current orientation as a (x, y, z, w)... | def get_orientation_error(target_orn, current_orn):
current_orn = np.array(
[current_orn[3], current_orn[0], current_orn[1], current_orn[2]]
)
target_orn = np.array([target_orn[3], target_orn[0], target_orn[1], target_orn[2]])
pinv = np.zeros((3, 4))
pinv[0, :] = [-current_orn[1], curr... | 324,713 |
Computes the error corresponding to target pose - current pose as a 6-dim vector.
The first 3 components correspond to translational error while the last 3 components
correspond to the rotational error.
Args:
target_pose: a 4x4 homogenous matrix for the target pose
current_pose: a 4x4 homog... | def get_pose_error(target_pose, current_pose):
error = np.zeros(6)
# compute translational error
target_pos = target_pose[:3, 3]
current_pos = current_pose[:3, 3]
pos_err = target_pos - current_pos
# compute rotational error
r1 = current_pose[:3, 0]
r2 = current_pose[:3, 1]
r3... | 324,714 |
Finds contact between two geom groups.
Args:
geoms_1: a list of geom names (string)
geoms_2: another list of geom names (string)
Returns:
iterator of all contacts between @geoms_1 and @geoms_2 | def find_contacts(self, geoms_1, geoms_2):
for contact in self.sim.data.contact[0 : self.sim.data.ncon]:
# check contact geom in geoms
c1_in_g1 = self.sim.model.geom_id2name(contact.geom1) in geoms_1
c2_in_g2 = self.sim.model.geom_id2name(contact.geom2) in geoms_2
... | 324,724 |
Overrides the superclass method to actuate the robot with the
passed joint velocities and gripper control.
Args:
action (numpy array): The control to apply to the robot. The first
@self.mujoco_robot.dof dimensions should be the desired
normalized joint velo... | def _pre_action(self, action):
# clip actions into valid range
assert len(action) == self.dof, "environment got invalid action dimension"
low, high = self.action_spec
action = np.clip(action, low, high)
if self.has_gripper:
arm_action = action[: self.mujoco... | 324,735 |
Playback data from an episode.
Args:
ep_dir: The path to the directory containing data for an episode. | def playback_trajectory(env, ep_dir):
# first reload the model from the xml
xml_path = os.path.join(ep_dir, "model.xml")
with open(xml_path, "r") as f:
env.reset_from_xml_string(f.read())
state_paths = os.path.join(ep_dir, "state_*.npz")
# read states back, load them one by one, and ... | 324,739 |
Loads client secrets from the given filename.
Args:
filename: The name of the file containing the JSON secret key.
Returns:
A 2-tuple, the first item containing the client id, and the second
item containing a client secret. | def _load_client_secrets(filename):
client_type, client_info = clientsecrets.loadfile(filename)
if client_type != clientsecrets.TYPE_WEB:
raise ValueError(
'The flow specified in {} is not supported, only the WEB flow '
'type is supported.'.format(client_type))
return ... | 325,477 |
Initializes client id and client secret based on the settings.
Args:
settings_instance: An instance of ``django.conf.settings``.
Returns:
A 2-tuple, the first item is the client id and the second
item is the client secret. | def _get_oauth2_client_id_and_secret(settings_instance):
secret_json = getattr(settings_instance,
'GOOGLE_OAUTH2_CLIENT_SECRETS_JSON', None)
if secret_json is not None:
return _load_client_secrets(secret_json)
else:
client_id = getattr(settings_instance, "GOOGL... | 325,478 |
Gets a Credentials storage object provided by the Django OAuth2 Helper
object.
Args:
request: Reference to the current request object.
Returns:
An :class:`oauth2.client.Storage` object. | def get_storage(request):
storage_model = oauth2_settings.storage_model
user_property = oauth2_settings.storage_model_user_property
credentials_property = oauth2_settings.storage_model_credentials_property
if storage_model:
module_name, class_name = storage_model.rsplit('.', 1)
mod... | 325,480 |
Helper method to create a redirect response with URL params.
This builds a redirect string that converts kwargs into a
query string.
Args:
url_name: The name of the url to redirect to.
kwargs: the query string param and their values to build.
Returns:
A properly formatted redi... | def _redirect_with_params(url_name, *args, **kwargs):
url = urlresolvers.reverse(url_name, args=args)
params = parse.urlencode(kwargs, True)
return "{0}?{1}".format(url, params) | 325,481 |
Initialize the Oauth2 Object.
Args:
request: Django request object.
scopes: Scopes desired for this OAuth2 flow.
return_url: The url to return to after the OAuth flow is complete,
defaults to the request's current URL path. | def __init__(self, request, scopes=None, return_url=None):
self.request = request
self.return_url = return_url or request.get_full_path()
if scopes:
self._scopes = set(oauth2_settings.scopes) | set(scopes)
else:
self._scopes = set(oauth2_settings.scopes) | 325,484 |
Write a credentials to the SQLAlchemy datastore.
Args:
credentials: :class:`oauth2client.Credentials` | def locked_put(self, credentials):
filters = {self.key_name: self.key_value}
query = self.session.query(self.model_class).filter_by(**filters)
entity = query.first()
if not entity:
entity = self.model_class(**filters)
setattr(entity, self.property_name, cre... | 325,489 |
Deserialize a JSON-serialized instance.
Inverse to :meth:`to_json`.
Args:
json_data: dict or string, Serialized JSON (as a string or an
already parsed dictionary) representing a credential.
Returns:
ServiceAccountCredentials from the serialized d... | def from_json(cls, json_data):
if not isinstance(json_data, dict):
json_data = json.loads(_helpers._from_bytes(json_data))
private_key_pkcs8_pem = None
pkcs12_val = json_data.get(_PKCS12_KEY)
password = None
if pkcs12_val is None:
private_key_pkc... | 325,504 |
Create credentials that specify additional claims.
Args:
claims: dict, key-value pairs for claims.
Returns:
ServiceAccountCredentials, a copy of the current service account
credentials with updated claims to use when obtaining access
tokens. | def create_with_claims(self, claims):
new_kwargs = dict(self._kwargs)
new_kwargs.update(claims)
result = self.__class__(self._service_account_email,
self._signer,
scopes=self._scopes,
private... | 325,506 |
Create a signed jwt.
Args:
http: unused
additional_claims: dict, additional claims to add to
the payload of the JWT.
Returns:
An AccessTokenInfo with the signed jwt | def get_access_token(self, http=None, additional_claims=None):
if additional_claims is None:
if self.access_token is None or self.access_token_expired:
self.refresh(None)
return client.AccessTokenInfo(
access_token=self.access_token, expires_in=self... | 325,508 |
Saves a file with read-write permissions on for the owner.
Args:
filename: String. Absolute path to file.
json_contents: JSON serializable object to be saved. | def _save_private_file(filename, json_contents):
temp_filename = tempfile.mktemp()
file_desc = os.open(temp_filename, os.O_WRONLY | os.O_CREAT, 0o600)
with os.fdopen(file_desc, 'w') as file_handle:
json.dump(json_contents, file_handle, sort_keys=True,
indent=2, separators=(','... | 325,515 |
Save the provided GoogleCredentials to the well known file.
Args:
credentials: the credentials to be saved to the well known file;
it should be an instance of GoogleCredentials
well_known_file: the name of the file where the credentials are to be
saved;... | def save_to_well_known_file(credentials, well_known_file=None):
# TODO(orestica): move this method to tools.py
# once the argparse import gets fixed (it is not present in Python 2.6)
if well_known_file is None:
well_known_file = _get_well_known_file()
config_dir = os.path.dirname(well_kno... | 325,516 |
Extract the JSON payload from a JWT.
Does the extraction w/o checking the signature.
Args:
id_token: string or bytestring, OAuth 2.0 id_token.
Returns:
object, The deserialized JSON payload. | def _extract_id_token(id_token):
if type(id_token) == bytes:
segments = id_token.split(b'.')
else:
segments = id_token.split(u'.')
if len(segments) != 3:
raise VerifyJwtTokenError(
'Wrong number of segments in token: {0}'.format(id_token))
return json.loads(
... | 325,521 |
Parses response of an exchange token request.
Most providers return JSON but some (e.g. Facebook) return a
url-encoded string.
Args:
content: The body of a response
Returns:
Content as a dictionary object. Note that the dict could be empty,
i.e. {}. That basically indicates a ... | def _parse_exchange_token_response(content):
resp = {}
content = _helpers._from_bytes(content)
try:
resp = json.loads(content)
except Exception:
# different JSON libs raise different exceptions,
# so we just do a catch-all here
resp = _helpers.parse_unique_urlencoded... | 325,522 |
Utility class method to instantiate a Credentials subclass from JSON.
Expects the JSON string to have been produced by to_json().
Args:
json_data: string or bytes, JSON from to_json().
Returns:
An instance of the subclass of Credentials that was serialized with
... | def new_from_json(cls, json_data):
json_data_as_unicode = _helpers._from_bytes(json_data)
data = json.loads(json_data_as_unicode)
# Find and call the right classmethod from_json() to restore
# the object.
module_name = data['_module']
try:
module_obj ... | 325,529 |
Write a credential.
The Storage lock must be held when this is called.
Args:
credentials: Credentials, the credentials to store. | def put(self, credentials):
self.acquire_lock()
try:
self.locked_put(credentials)
finally:
self.release_lock() | 325,530 |
Instantiate a Credentials object from a JSON description of it.
The JSON should have been produced by calling .to_json() on the object.
Args:
json_data: string or bytes, JSON to deserialize.
Returns:
An instance of a Credentials subclass. | def from_json(cls, json_data):
data = json.loads(_helpers._from_bytes(json_data))
if (data.get('token_expiry') and
not isinstance(data['token_expiry'], datetime.datetime)):
try:
data['token_expiry'] = datetime.datetime.strptime(
da... | 325,533 |
Refreshes the access_token.
This method first checks by reading the Storage object if available.
If a refresh is still needed, it holds the Storage lock until the
refresh is completed.
Args:
http: an object to be used to make HTTP requests.
Raises:
Http... | def _refresh(self, http):
if not self.store:
self._do_refresh_request(http)
else:
self.store.acquire_lock()
try:
new_cred = self.store.locked_get()
if (new_cred and not new_cred.invalid and
new_cred.acc... | 325,538 |
Refresh the access_token using the refresh_token.
Args:
http: an object to be used to make HTTP requests.
Raises:
HttpAccessTokenRefreshError: When the refresh fails. | def _do_refresh_request(self, http):
body = self._generate_refresh_request_body()
headers = self._generate_refresh_request_headers()
logger.info('Refreshing access_token')
resp, content = transport.request(
http, self.token_uri, method='POST',
body=body,... | 325,539 |
Revokes this credential and deletes the stored copy (if it exists).
Args:
http: an object to be used to make HTTP requests. | def _revoke(self, http):
self._do_revoke(http, self.refresh_token or self.access_token) | 325,540 |
Revokes this credential and deletes the stored copy (if it exists).
Args:
http: an object to be used to make HTTP requests.
token: A string used as the token to be revoked. Can be either an
access_token or refresh_token.
Raises:
TokenRevokeError: ... | def _do_revoke(self, http, token):
logger.info('Revoking token')
query_params = {'token': token}
token_revoke_uri = _helpers.update_query_params(
self.revoke_uri, query_params)
resp, content = transport.request(http, token_revoke_uri)
if resp.status == http_c... | 325,541 |
Retrieves the list of authorized scopes from the OAuth2 provider.
Args:
http: an object to be used to make HTTP requests.
token: A string used as the token to identify the credentials to
the provider.
Raises:
Error: When refresh fails, indicating ... | def _do_retrieve_scopes(self, http, token):
logger.info('Refreshing scopes')
query_params = {'access_token': token, 'fields': 'scope'}
token_info_uri = _helpers.update_query_params(
self.token_info_uri, query_params)
resp, content = transport.request(http, token_info... | 325,542 |
Create an instance of OAuth2Credentials
This is one of the few types if Credentials that you should contrust,
Credentials objects are usually instantiated by a Flow.
Args:
access_token: string, access token.
user_agent: string, The HTTP User-Agent to provide for this
... | def __init__(self, access_token, user_agent, revoke_uri=None):
super(AccessTokenCredentials, self).__init__(
access_token,
None,
None,
None,
None,
None,
user_agent,
revoke_uri=revoke_uri) | 325,543 |
Create a Credentials object by reading information from a file.
It returns an object of type GoogleCredentials.
Args:
credential_filename: the path to the file from where the
credentials are to be read
Raises:
ApplicationDefaultCredenti... | def from_stream(credential_filename):
if credential_filename and os.path.isfile(credential_filename):
try:
return _get_application_default_credential_from_file(
credential_filename)
except (ApplicationDefaultCredentialsError, ValueError) as er... | 325,549 |
Verifies a message against a signature.
Args:
message: string or bytes, The message to verify. If string, will be
encoded to bytes as utf-8.
signature: string or bytes, The signature on the message. If
string, will be encoded to bytes as utf-8... | def verify(self, message, signature):
message = _helpers._to_bytes(message, encoding='utf-8')
try:
return rsa.pkcs1.verify(message, signature, self._pubkey)
except (ValueError, rsa.pkcs1.VerificationError):
return False | 325,557 |
Signs a message.
Args:
message: bytes, Message to be signed.
Returns:
string, The signature of the message for the given key. | def sign(self, message):
message = _helpers._to_bytes(message, encoding='utf-8')
return rsa.pkcs1.sign(message, self._key, 'SHA-256') | 325,559 |
Construct an RsaSigner instance from a string.
Args:
key: string, private key in PEM format.
password: string, password for private key file. Unused for PEM
files.
Returns:
RsaSigner instance.
Raises:
ValueError if the key ... | def from_string(cls, key, password='notasecret'):
key = _helpers._from_bytes(key) # pem expects str in Py3
marker_id, key_bytes = pem.readPemBlocksFromFile(
six.StringIO(key), _PKCS1_MARKER, _PKCS8_MARKER)
if marker_id == 0:
pkey = rsa.key.PrivateKey.load_pkcs1... | 325,560 |
Writes credentials to a file.
Refer to :func:`_load_credentials_file` for the format.
Args:
credentials_file: An open file handle, must be read/write.
credentials: A dictionary mapping user-defined keys to an instance of
:class:`oauth2client.client.Credentials`. | def _write_credentials_file(credentials_file, credentials):
data = {'file_version': 2, 'credentials': {}}
for key, credential in iteritems(credentials):
credential_json = credential.to_json()
encoded_credential = _helpers._from_bytes(base64.b64encode(
_helpers._to_bytes(credent... | 325,563 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.