Python Forum
Code completion for namedtupe-based object in PyCharm
Thread Rating:
  • 3 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code completion for namedtupe-based object in PyCharm
#1
Hi,
I am defining an Enum-style object as a wrapper for string constants
import time, re, collections
def make_enum(*enum_args, **enum_kwargs):
  _tokenizer = lambda s: re.sub('\W+', '_', s)
  enum_kwargs = {k.upper(): v for k, v in enum_kwargs.items()}

  enum_kwargs.update([(_tokenizer(s).upper(), s) for s in enum_args])
  return collections.namedtuple('_C_{:x}'.format(hash(time.time())), enum_kwargs.keys())(**enum_kwargs)

C = make_enum('true', 'false', sentence='This is long')


My only issue - PyCharm does not recognize the structure of the generated object. Is there some way to force auto-completion for that kind of objects?
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#2
I don't know much about pycharm, but it seems like it'd be difficult to have autocompletion for dynamically generated classes, unless it was parsing and running your file as you were typing or something.

I was thinking maybe it could work if you used an annotation to offer a type hint for it, but I can't test it since I don't pycharm.
import time, re, collections
def make_enum(*enum_args, **enum_kwargs):
 _tokenizer = lambda s: re.sub('\W+', '_', s)
 enum_kwargs = {k.upper(): v for k, v in enum_kwargs.items()}

 enum_kwargs.update([(_tokenizer(s).upper(), s) for s in enum_args])
 enum = collections.namedtuple('_C_{:x}'.format(hash(time.time())), enum_kwargs.keys())
 def generate() -> enum:
     return enum(**enum_kwargs)
 return generate

builder = make_enum('true', 'false', sentence='This is long')
C = builder()
print(C)
print(type(C))
Reply
#3
@nilamo Thanks, I will give it a try
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
I don't use pycharm, but you might see if you have better luck with the built-in enum.
Reply
#5
(May-01-2017, 07:06 PM)micseydel Wrote: I don't use pycharm, but you might see if you have better luck with the built-in enum.
Built-in enum requires value qualification when used in
  • os.path functions
  • Robot FrameWork
After adding qualifier for the n-th time I decided that it - enum - sucks, and dropped the bloody thing Wall
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Run code like cell pycharm arsouzaesilva 3 1,804 Sep-20-2021, 12:05 PM
Last Post: Larz60+
  This is an Object - Code interpretation JoeDainton123 2 1,767 Jun-16-2021, 08:11 PM
Last Post: snippsat
  name error with text based rpg code cris_ram415 4 2,377 Oct-25-2020, 05:44 AM
Last Post: bowlofred
  Multiprocessing Module Running An Infinite Child Process Even After Completion hsikora 0 3,835 Dec-19-2018, 08:01 AM
Last Post: hsikora
  Subprocess.send_signal, wait until completion plinio 5 8,906 Jun-29-2018, 12:07 PM
Last Post: plinio
  Code review for S3 object creation beherap 0 2,114 Mar-29-2018, 01:31 PM
Last Post: beherap
  The number of object makes code slower? fig0 1 2,466 Jan-25-2018, 11:16 PM
Last Post: Gribouillis
  pycharm doesn't offer option to run code meems 9 9,956 Jul-29-2017, 08:01 PM
Last Post: meems
  iPython tab completion of method variable sixteenornumber 1 3,194 Apr-04-2017, 02:14 PM
Last Post: sixteenornumber

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020