Opened 13 years ago

Closed 12 years ago

#17061 closed Cleanup/optimization (fixed)

Factor out "get a module-level object from a dotted path" function

Reported by: Carl Meyer Owned by: Claude Paroz
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: james@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We've got importlib.import_module, but we're missing a higher-level function that would take "x.y.z.foo" (and maybe a fallback default?) and return the actual object "foo" from the "x.y.z" module. Instead we've got the same .rsplit(1) code duplicated everywhere we need to do this.

One implementation we could steal is here: https://github.com/erikrose/django-tidings/blob/master/tidings/utils.py#L144

I was sure there was already a ticket for this, but can't seem to find it, so maybe I was wrong...

Attachments (2)

17061-1.diff (36.4 KB ) - added by Claude Paroz 12 years ago.
17061-2.diff (35.8 KB ) - added by Claude Paroz 12 years ago.
Updated to current trunk

Download all attachments as: .zip

Change History (9)

comment:1 by James Socol, 13 years ago

Cc: james@… added

comment:2 by Claude Paroz, 12 years ago

Owner: changed from nobody to Claude Paroz

by Claude Paroz, 12 years ago

Attachment: 17061-1.diff added

comment:3 by Claude Paroz, 12 years ago

Has patch: set

26 files changed, 133 insertions(+), 273 deletions(-)

by Claude Paroz, 12 years ago

Attachment: 17061-2.diff added

Updated to current trunk

comment:4 by Claude Paroz, 12 years ago

My last doubt is about the name of the function. I've initially chosen import_module_from_path, but should we name it import_object_from_path orimport_attribute_from_path as the imported name is not the module itself, but an attribute of the module (variable, function, class, ...)?

in reply to:  4 comment:5 by Carl Meyer, 12 years ago

Replying to claudep:

My last doubt is about the name of the function. I've initially chosen import_module_from_path, but should we name it import_object_from_path orimport_attribute_from_path as the imported name is not the module itself, but an attribute of the module (variable, function, class, ...)?

How about just import_from_path or import_by_path - since we really don't know what type of thing we are importing.

comment:6 by Claude Paroz, 12 years ago

Oh, yes, I like import_by_path.

comment:7 by Claude Paroz <claude@…>, 12 years ago

Resolution: fixed
Status: newclosed

In 7c5b244826be636429791a8ca76b2adc678e82e7:

Fixed #17061 -- Factored out importing object from a dotted path

Thanks Carl Meyer for the report.

Note: See TracTickets for help on using tickets.
Back to Top