I want to add an helper function to my yesod app that will "live" in a seperate file and could be imported in both foundation file and the handlers, i dont want to have reimport all the modules again. and i cant use the Import module because im getting circular dependencies ( Import -> helper -> foundation -> Import).
The GHC manual has a section on mutually recursive modules.
I think rather than messing around with mutually-recursive modules it might be better to just re-import all the stuff from Import that you want.
Related
I'm working on migrating my Firebase Cloud Functions project from JavaScript to TypeScript.
In my existing codebase, to reduce the time taken by cold starts and to be efficient with resources, I would import necessary modules/files within the specific functions that require them, instead of importing at the top-level. I would do it via something like const myPackage = require('helpful-module');.
Now that I'm using TypeScript, I would like my IDE to recognise the available methods and attributes on the code I import. I find that using the require function does not do that, and only when using the import statement do I get the autocompletion I want.
The problem is that when I put the import statement at the top of the individual function that requires it, I get the following error:
An import declaration can only be used at the top level of a namespace
or module.
How can I get TypeScript autocomplete on imported files, while also importing only within the functions that require that file/module?
Is there any way to create my own module in qpython3 ? If there is, it would be great to fix my code properly, without going all the way down to fix just onde line.
well, i'm creating a game with 3 games inside it and i would like to put the game functions inside individual modules, like:
from tictactoe import
structureTictactoe
from Chess import structureChess
then these functions when called simply print the specific game structure like the tictactoe grade and the chess table. For instance, it's simpler to edit the game functions inside individual modules
You have to write the functions that you want into a .py script that has the same name as you want the module to be called. You then have to add that into the site packages directory and then you should be able to access them from anywhere.
Just make sure that it is in the "qpython\lib\python3.2\packages\" directory
create your game library, such as myGameLib.py in your script's local directory.
Then from your main python code:
import from myGameLib *
Note that this will work. However, if you want to create a sub directory for your python lib, such as a local directory to your script, /mysubdir,
import from mysubdir.myGameLib *
appears to be broken in QPython3.
Is there a way to have custom behaviour for import statements in Python? How? E.g.:
import "https://github.com/kennethreitz/requests"
import requests#2.11.1
import requests#7322a09379565bbeba9bb40000b41eab8856352e
Alternatively, in case this isn't possible... Can this be achieved in a standard way with function calls? How? E.g.:
import gitloader
repo = gitloader.repo("https://github.com/kennethreitz/requests")
requests = repo.from_commit("7322a09379565bbeba9bb40000b41eab8856352e")
There are two reasons for why I would like to do this. The first reason is convenience (Golang style imports). The second reason is that I need cryptographic verification of plugin modules for a project. I'm using Python 3.x
What you're essentially asking is customization of the import steps. This was made possible with PEP 302 which brought about certain hooks for for customization.
That PEP is currently not the source from which you should learn how importing works; rather, look at the documentation for the import statement for reference. There it states:
Python includes a number of default finders and importers. The first one knows how to locate built-in modules, and the second knows how to locate frozen modules. A third default finder searches an import path for modules. The import path is a list of locations that may name file system paths or zip files. It can also be extended to search for any locatable resource, such as those identified by URLs.
The import machinery is extensible, so new finders can be added to extend the range and scope of module searching.
In short, you'll need to define the appropriate finder (to find the modules you're looking for) and an appropriate loader for loading these.
Is there any way to extend library module in Haskell?
For example, I would like to add firstToLower function to Data.String. When I create my own Data.String it masks library one:
module Data.String where
import Prelude
import Data.Char (toLower)
firstToLower :: String -> String
firstToLower (c:cs) = toLower c : cs
firstToLower "" = ""
Then I get error trying to import Data.String (lines):
Module `Data.String' does not export `lines'
It would be really nice if such thing is possible. If not, what are best practices for such situations? Where such extensions should be placed?
Thank you.
Update
I don't plan to release my extensions as a library, just want to organize it inside my project in a meaningful way.
No, this is not possible. One solution that people have used in the past is to put your additions in a module with a name like Data.String.Extra and release that module on Hackage (if you think that your additions could be useful to other people).
Alternatively, you can propose your extension for inclusion in the standard library.
I use haddock and don't want all of my exported functions to be displayed in the documentation. Is it possible to hide specific functions?
I found the prune attribute at http://www.haskell.org/haddock/doc/html/module-attributes.html, but this is not what I want since some functions which shall be exported don't have a documentation annotation.
Assuming your current module is Foo.Bar, one solution would be to break it up into Foo.Bar and Foo.Bar.Internal. You could move all the definitions related to the function you don't want to export--perhaps even all the definitions--into Foo.Bar.Internal. Then, in Foo.Bar, you would re-export only the definitions you want the world to see.
This approach has a couple of advantages. It lets you export everything you need, while still giving the user a clear sign that certain things shouldn't be used. It also lets you document your special functions inside the Internal module, which is going to be useful (if only for your future self :P).
You could simply not export Foo.Bar.Internal in your .cabal file, hiding it from the world. However, this is not necessarily the best approach; look at the answers to How, why and when to use the ".Internal" modules pattern?, in particular luqui's.
Another possibility is to make a module Foo.Bar.Hidden exporting all of the stuff you want to hide, and then re-export the whole Foo.Bar.Hidden module from Foo.Bar:
module Foo.Bar (
blah1,
blah2,
blah3,
module Foo.Bar.Hidden
)
where
import Foo.Bar.Hidden
blah1 = ...
blah2 = ...
blah3 = ...
This way, the hidden stuff will be exported from Foo.Bar, but not included in the documentation. The documentation will only include one relatively unobtrusive link to the Foo.Bar.Hidden module as a whole.