Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid creating extra memory with lists #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyasn1/compat/octets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
if version_info[0] <= 2:
int2oct = chr
# noinspection PyPep8
ints2octs = lambda s: ''.join([int2oct(x) for x in s])
ints2octs = lambda s: ''.join(int2oct(x) for x in s)
null = ''
oct2int = ord
# TODO: refactor to return a sequence of ints
Expand Down
4 changes: 2 additions & 2 deletions pyasn1/type/char.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def prettyIn(self, value):
elif isinstance(value, str):
return value.decode(self.encoding)
elif isinstance(value, (tuple, list)):
return self.prettyIn(''.join([chr(x) for x in value]))
return self.prettyIn(''.join(chr(x) for x in value))
elif isinstance(value, univ.OctetString):
return value.asOctets().decode(self.encoding)
else:
Expand All @@ -94,7 +94,7 @@ def asOctets(self, padding=True):
return str(self)

def asNumbers(self, padding=True):
return tuple([ord(x) for x in str(self)])
return tuple(ord(x) for x in str(self))

else:
def __str__(self):
Expand Down
17 changes: 8 additions & 9 deletions pyasn1/type/namedtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,19 @@ def __init__(self, *namedTypes, **kwargs):
self.__ambiguousTypes = 'terminal' not in kwargs and self.__computeAmbiguousTypes() or {}
self.__uniqueTagMap = self.__computeTagMaps(unique=True)
self.__nonUniqueTagMap = self.__computeTagMaps(unique=False)
self.__hasOptionalOrDefault = any([True for namedType in self.__namedTypes
if namedType.isDefaulted or namedType.isOptional])
self.__hasOpenTypes = any([True for namedType in self.__namedTypes
if namedType.openType])
self.__hasOptionalOrDefault = any(namedType.isDefaulted or namedType.isOptional
for namedType in self.__namedTypes)
self.__hasOpenTypes = any(namedType.openType for namedType in self.__namedTypes)

self.__requiredComponents = frozenset(
[idx for idx, nt in enumerate(self.__namedTypes) if not nt.isOptional and not nt.isDefaulted]
idx for idx, nt in enumerate(self.__namedTypes) if not nt.isOptional and not nt.isDefaulted
)
self.__keys = frozenset([namedType.name for namedType in self.__namedTypes])
self.__values = tuple([namedType.asn1Object for namedType in self.__namedTypes])
self.__items = tuple([(namedType.name, namedType.asn1Object) for namedType in self.__namedTypes])
self.__keys = frozenset(namedType.name for namedType in self.__namedTypes)
self.__values = tuple(namedType.asn1Object for namedType in self.__namedTypes)
self.__items = tuple((namedType.name, namedType.asn1Object) for namedType in self.__namedTypes)

def __repr__(self):
representation = ', '.join(['%r' % x for x in self.__namedTypes])
representation = ', '.join('%r' % x for x in self.__namedTypes)
return '<%s object, types %s>' % (
self.__class__.__name__, representation)

Expand Down
2 changes: 1 addition & 1 deletion pyasn1/type/namedval.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def __init__(self, *args, **kwargs):
number += 1

def __repr__(self):
representation = ', '.join(['%s=%d' % x for x in self.items()])
representation = ', '.join('%s=%d' % x for x in self.items())

if len(representation) > 64:
representation = representation[:32] + '...' + representation[-32:]
Expand Down
4 changes: 2 additions & 2 deletions pyasn1/type/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ def __init__(self, baseTag=(), *superTags):
self.__hash = hash(self.__superTagsClassId)

def __repr__(self):
representation = '-'.join(['%s:%s:%s' % (x.tagClass, x.tagFormat, x.tagId)
for x in self.__superTags])
representation = '-'.join('%s:%s:%s' % (x.tagClass, x.tagFormat, x.tagId)
for x in self.__superTags)
if representation:
representation = 'tags ' + representation
else:
Expand Down
12 changes: 6 additions & 6 deletions pyasn1/type/univ.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def prettyIn(self, value):
return self.fromBinaryString(value, internalFormat=True)

elif isinstance(value, (tuple, list)):
return self.fromBinaryString(''.join([b and '1' or '0' for b in value]), internalFormat=True)
return self.fromBinaryString(''.join(b and '1' or '0' for b in value), internalFormat=True)

elif isinstance(value, BitString):
return SizedInteger(value).setBitLength(len(value))
Expand Down Expand Up @@ -839,7 +839,7 @@ def prettyIn(self, value):

elif isinstance(value, (tuple, list)):
try:
return ''.join([chr(x) for x in value])
return ''.join(chr(x) for x in value)

except ValueError:
raise error.PyAsn1Error(
Expand Down Expand Up @@ -867,7 +867,7 @@ def asOctets(self):
return str(self._value)

def asNumbers(self):
return tuple([ord(x) for x in self._value])
return tuple(ord(x) for x in self._value)

else:
def prettyIn(self, value):
Expand Down Expand Up @@ -1221,14 +1221,14 @@ def prettyIn(self, value):
'Malformed Object ID %s at %s: %s' % (value, self.__class__.__name__, sys.exc_info()[1])
)
try:
return tuple([int(subOid) for subOid in value.split('.') if subOid])
return tuple(int(subOid) for subOid in value.split('.') if subOid)
except ValueError:
raise error.PyAsn1Error(
'Malformed Object ID %s at %s: %s' % (value, self.__class__.__name__, sys.exc_info()[1])
)

try:
tupleOfInts = tuple([int(subOid) for subOid in value if subOid >= 0])
tupleOfInts = tuple(int(subOid) for subOid in value if subOid >= 0)

except (ValueError, TypeError):
raise error.PyAsn1Error(
Expand All @@ -1241,7 +1241,7 @@ def prettyIn(self, value):
raise error.PyAsn1Error('Malformed Object ID %s at %s' % (value, self.__class__.__name__))

def prettyOut(self, value):
return '.'.join([str(x) for x in value])
return '.'.join(str(x) for x in value)


class Real(base.SimpleAsn1Type):
Expand Down