Python Forum
extracting a function/class/method from code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extracting a function/class/method from code
#1
i'm not sure how to look for this. i would like a tool that can be given a file (or stdin) with code and the name of a function or class or method which will extract that function or class or method and output it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
For what purpose?
Reply
#3
Are you looking for inspect.getsource() ?
Reply
#4
(Mar-25-2022, 07:06 AM)Gribouillis Wrote: Are you looking for inspect.getsource() ?
almost. if the code i want to extract from can be imported as a module, this could work. for stdin i'd have to copy it to a file, first, unless there is a way to import from stdin or a list of str or a big str.
(Mar-25-2022, 06:27 AM)ndc85430 Wrote: For what purpose?
to extract source code of a function or class to the end of that function or class, comiing from a file or stdin. this is to find the end of that function or class. maybe parsing indentation (after disposing of comments) would be the easy way.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Are you saying that your functions/classes are so big that it's difficult to find where they end? If so, refactor to make them simpler - it sounds like there's a lot going on in these things which makes them difficult to understand. Clean Code has good advice on the sorts of things you want to go for.

If that's not what you mean, please clarify.
Also, what kind of editor/IDE are you using? They tend to at least let you collapse (and expand) blocks and should be able to show you where the ends are. Note that most of my experience is with using IntelliJ for Java/Scala/Kotlin, but I assume that the tooling is there for Python in other editors.

Still, I strongly suggest refactoring these large things.
Reply
#6
(Mar-25-2022, 06:38 PM)ndc85430 Wrote: If that's not what you mean, please clarify.

they are big relative to code extracting source code. i cannot refactor the whole code. i must extract the functions and classes, first.
(Mar-25-2022, 06:38 PM)ndc85430 Wrote: Also, what kind of editor/IDE are you using?
emacs/bash within a nearly full screen xterm (i use the same for C and everything else).
(Mar-25-2022, 06:38 PM)ndc85430 Wrote: Still, I strongly suggest refactoring these large things.
refactoring each function/class makes more sense once they are isolated from each other.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function/method keyword argument alias Skaperen 1 3,120 Dec-01-2019, 03:56 AM
Last Post: Gribouillis
  omitting arguments in function/method calls Skaperen 10 3,973 Nov-24-2019, 10:13 PM
Last Post: Gribouillis
  writing a class or a function Skaperen 9 4,424 Dec-30-2018, 04:30 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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