How can I tell a Racket program to load optional configuration code?

interstar

I'm currently loading some configuration values into my program using

(require "config.rkt")

However, this requires that config.rkt is there. I'd like to be able to put some default values into my program and have Racket override them only if there's a local config file.

In Python I'd do something like :

try :
    from local_configs import MAIN_PATH, MAX_ENTRIES
except :
    MAIN_PATH = "~"
    MAX_ENTRIES = 20

What's the equivalent in Racket?

Asumu Takikawa

You can use dynamic-require along with an exception handler (with-handlers).

Edit: my first proposed solution (using a failure thunk for dynamic-require) didn't handle the case where "config.rkt" didn't exist at all. Adding exception handling solves this.

Example:

#lang racket

;; try to require the given name, returning default on failure
(define (get-config name default)
  (with-handlers ([exn:fail? (λ (e) default)])
    (dynamic-require "config.rkt" name)))

(define main-path   (get-config 'main-path "~"))
(define max-entries (get-config 'max-entries 20))

;; just to show what the values are
(list main-path max-entries)

Here's a sample config file:

#lang racket

(provide main-path)

(define main-path "/home")

Running the first module with the sample config module produces a result like this:

'("/home" 20)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I tell a Racket program to load optional configuration code?

From Dev

How can I tell if a Perl module is actually used in my program?

From Dev

How can I tell state of program seemingly running

From Dev

How can I tell that my program is under unit test environment

From Dev

How can I tell if an OSX program is running in the debugger?

From Dev

how can I tell in java if the program is running on a windows or linux machine?

From Dev

How can I tell if my <List> is being used by my program?

From Dev

How can I tell if Trigger code is being executed at a Replication Subscriber?

From Dev

How can I minimize the code size of this program?

From Dev

How can I load optimized code in GHCI?

From Dev

Can you tell me how this program is working?

From Dev

How can I tell if a program I write is 32 bit or 64 bit?

From Dev

How can this racket code for an anaphoric -> or ->> macro be improved?

From Dev

How can I load a program icon in C#

From Dev

How would I make this Racket code DRYer?

From Dev

How can I tell if an app was purchased using Apple's volume purchase program for education?

From Dev

Racket : How can I turn a stream into a list?

From Dev

How can I put a plot on GUI in Racket?

From Dev

Racket : How can I turn a stream into a list?

From Dev

How can I interact with a website through Racket

From Dev

how can I match syntax in racket?

From Dev

How can I map an optional into a primitive optional?

From Dev

How to tell symfony to load my custom routing.yml configuration

From Dev

How can we tell Hibernate to make identifier properties as optional

From Dev

How can I tell if cuda code is being compiled with relocatable device code?

From Dev

How can I tell if cuda code is being compiled with relocatable device code?

From Dev

Can anyone tell how can i put getter and setter on the below code, and what exactly getter and setter do

From Dev

How can I tell maven where I want the compiled code to go?

From Dev

TBB Linker error How can I tell if I am missing an include or if the code is outdated

Related Related

  1. 1

    How can I tell a Racket program to load optional configuration code?

  2. 2

    How can I tell if a Perl module is actually used in my program?

  3. 3

    How can I tell state of program seemingly running

  4. 4

    How can I tell that my program is under unit test environment

  5. 5

    How can I tell if an OSX program is running in the debugger?

  6. 6

    how can I tell in java if the program is running on a windows or linux machine?

  7. 7

    How can I tell if my <List> is being used by my program?

  8. 8

    How can I tell if Trigger code is being executed at a Replication Subscriber?

  9. 9

    How can I minimize the code size of this program?

  10. 10

    How can I load optimized code in GHCI?

  11. 11

    Can you tell me how this program is working?

  12. 12

    How can I tell if a program I write is 32 bit or 64 bit?

  13. 13

    How can this racket code for an anaphoric -> or ->> macro be improved?

  14. 14

    How can I load a program icon in C#

  15. 15

    How would I make this Racket code DRYer?

  16. 16

    How can I tell if an app was purchased using Apple's volume purchase program for education?

  17. 17

    Racket : How can I turn a stream into a list?

  18. 18

    How can I put a plot on GUI in Racket?

  19. 19

    Racket : How can I turn a stream into a list?

  20. 20

    How can I interact with a website through Racket

  21. 21

    how can I match syntax in racket?

  22. 22

    How can I map an optional into a primitive optional?

  23. 23

    How to tell symfony to load my custom routing.yml configuration

  24. 24

    How can we tell Hibernate to make identifier properties as optional

  25. 25

    How can I tell if cuda code is being compiled with relocatable device code?

  26. 26

    How can I tell if cuda code is being compiled with relocatable device code?

  27. 27

    Can anyone tell how can i put getter and setter on the below code, and what exactly getter and setter do

  28. 28

    How can I tell maven where I want the compiled code to go?

  29. 29

    TBB Linker error How can I tell if I am missing an include or if the code is outdated

HotTag

Archive