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
  data input while debugging causes unwanted code completion fred1232 2 2,586 Sep-14-2025, 03:32 PM
Last Post: deanhystad
  Code Completion in Visual Studio for External Libraries mjakov 0 1,257 Aug-25-2024, 02:48 PM
Last Post: mjakov
  Run code like cell pycharm arsouzaesilva 3 3,087 Sep-20-2021, 12:05 PM
Last Post: Larz60+
  This is an Object - Code interpretation JoeDainton123 2 3,056 Jun-16-2021, 08:11 PM
Last Post: snippsat
  name error with text based rpg code cris_ram415 4 3,938 Oct-25-2020, 05:44 AM
Last Post: bowlofred
  Multiprocessing Module Running An Infinite Child Process Even After Completion hsikora 0 5,865 Dec-19-2018, 08:01 AM
Last Post: hsikora
  Subprocess.send_signal, wait until completion plinio 5 13,585 Jun-29-2018, 12:07 PM
Last Post: plinio
  Code review for S3 object creation beherap 0 3,052 Mar-29-2018, 01:31 PM
Last Post: beherap
  The number of object makes code slower? fig0 1 3,455 Jan-25-2018, 11:16 PM
Last Post: Gribouillis
  pycharm doesn't offer option to run code meems 9 14,786 Jul-29-2017, 08:01 PM
Last Post: meems

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.